I found that JPA does not support the following Update:
Update Person p set p.name = :name_1 where p.id = :id_1,
p.name = :name_2 where p.id = :id_2,
p.name = :name_3 where p.id = :id_3
....
// It could go on, depending on the size of the input. Could be in 100s
So I have two options:
Option 1:
Query q = em.createQuery("Update Person p set p.name = :name where p.id = :id");
For ( int x=0; PersonsList.length; x++ ) {
// add name and id parameters
em.executeUpdate();
}
Questions:
- Is this all that's needed for Batch update? Anything else I need to add?
I set
hibernate.jdbc.batch_size", "20"
- Is the optimistic lock enabled here by default? (I do not have @Version in my entity though)
- What do I need to do in order to enforce Optimistic Locking, if not @Version?
Option 2:
Construct one single query using either Select Case
syntax or with Criteria API
Questions:
- Does the batching still happen here? (In a single big query)
- Is this better than the 1st approach in terms of performance?
- Whats the recommended approach out of these two options? Any other better approach?