spring dat jpa batch update using repository - spring-data-jpa

I'm using spring data jpa repository to handle my sql-server database, Now my problem is how to batch update to my table, because My table-java bean not contains all the columns from the table, so if I do repository.save(List<>), it may overwrite all the other columns to default value if they are not mapped in my java bean, so now, i just write like below to write native sql to update each column:
So how to batch update some fields?

In order to update a JPA entity object, you would have had to fetch it first. Once all the objects are fetched then you can update just the properties you want and save the List<Zip> in batch. This will only change the database columns that you specifically change.

Related

Entity Framework ValueGeneratedOnAdd with no requery

Say I have EntityFramework Core 2.1.14.
Say I have to integrate data into a legacy MySql database which was created by a self-taught.
Say also that one of the tables in this database has a surrogate key field that is generated on Add.
Say then that I want to use context.SaveChanges() to handle the Insert DML generation for me because I want to be lazy and because these tables are messy.
Say finally that I do not want Entity Framework to perform a follow-up query to retrieve this surrogate field; I don't care what it is. I just need it generated on insert.
How would one call context.SaveChanges() with an Added object having a property configured as .ValueGeneratedOnAdd() but also instruct Entity Framework to do nothing but generate my INSERT ? I don't want it to return the id.

How to sort by multiple properties and then Order By in Spring Data (JPA)

I am using spring data JPA and i´m trying to create a query to sort results by 2 names and then order by the First name.
Is it possible to do all that with only one query on spring data?
List<something> findByNameOrderByNameDesc(Collection<String> listNames); // I want to use some thing like this

JPA: Map a group of table columns extracted by native query to entity

I know that it is possible using #SqlResultSetMapping but I want to select not whole entity from the database but some fields and then map i to my entity using one of the constructor which accept that fields. Is that possible to map result with #EntityResult for only a few #FieldResult? I was trying to do that and all the time I get error which said that there is not specify mapping for some fields which exist in that entity.
The disadvantage of #SqlResultSetMapping is that you have to select all the columns.
The alternate way of doing this manually iterate over the DB result and populate your objects.
Well, if you are using JPA 1.0 your only option (not considering the manual mapping, of course), is to use #SqlResultSetMapping and map the whole table columns. With JPA 2.1 you can add a javax.persistence.ConstructorResult (see docs here) to map only the needed columns.

JPA select values from dynamicallay created table

I have some dynamically created tables(using native queries). So I do not have the names in my entity but stored in a db table. Now I want to retrieve datas from each. I want to use only JPA.
To use JPA you must have Entity that maps on to any table that you want to access. If you don't have that then JPA is not for you, and just use JDBC. Can't get simpler.

EF CodeFirst: Rails-style created and modified columns

Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how?
Thanks!
It is not supported in EF. EF will not create these columns for you automatically. You must do it yourselves by either:
Have Created and Modified properties in every entity where you want to maintain these values. You must also manually maintain these columns in your application (common approach is overriding SaveChanges and set values accordingly).
If you don't need these values mapped (you never expect to use them in your application and you are happy with the logic in the database) you can create custom database initializer which would execute your custom SQL to alter tables and add those columns, default constraints for Created columns and update triggers for Modified columns.