Jpa Optimistic locking without version attribute in database possible?

50 Views Asked by At

We can not change the data model and we are not allowed to add a version field on certain tables.

Also pessimistic locking isn't an option. Because:

  1. A row was read from the DB in one transaction and displayed in a web application.
  2. Maybe a hour later the user changes the data and try to save.
  3. Now we have a second transaction to save the changed data and an exception has to be thrown is if an other user or process changed the data meanwhile.

Be feet ist clear: We have to compare the original data with the actial data on the DB and save only if it's not changed.

But can jpa help to achieve that?

I tried to combine

@Version
@Transactional
int entityHash

And calculate the hash in

@PostLoad

But Hibernate internally stores the version before postload is called.

We can compare changed data manually but is there a way to use jpa functionality instead?

0

There are 0 best solutions below