Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Version is not updated in JPA in case of bulk-update named queries

Tags:

java

jpa

I am using openjpa 2.1.0 implementation of JPA.

Where I have entity in that have property annotated with @Version annotation.

    /** version column to control concurrency*/  
    @Column(name = "XXX_VERSION")  
    @Version  
    private Integer xxxVersion;  

here XXX_VERSION is column in table with datatype numeric, now when I call merge by editing entity then openjpa will do increment value from XXX_VERSION column by 1.

public E update(E entity) {  
        return entityManager.merge(entity);  
    }  

Above functionality as expected, working absolutely fine.

But when I used a named query for updating some of the columns from table as below:

Query query = getEntityManager().createNamedQuery(update query passed here without version column in it);
 query.setParameter(PaymentConstants.QUERY_PARAM_XXX_ID, Long.valueOf(XXXId));
 query.executeUpdate();

then version is not getting incremented due to that if any other concurrent user doing anything with same row from DB from session of him then version is not got incremented in XXX_VERSION column.

I have tried to pass the incremented version to update query, it throws an exception saying invalid query syntax.

What could be done so that version got incremented in case update queries using named query?

like image 809
Shailesh Narkhede Avatar asked Feb 13 '14 09:02

Shailesh Narkhede


1 Answers

I am afraid this is JPA compliant. Here is the excerpt from the JPA spec (4.10 Bulk Update and Delete Operations):

Bulk update maps directly to a database update operation, bypassing optimistic locking checks. Portable applications must manually update the value of the version column, if desired, and/or manually validate the value of the version column.

So you will have to do that manually: fetch the entries and save, one at each time.

like image 141
V G Avatar answered Nov 08 '22 14:11

V G