Best update strategy for hibernate stalestateException

we use sleep mode with optimistic locking. All of our entities have @version annotation.

This works fine if the user tries to keep an obsolete object, we get an incomplete exception. In our case, we would like to provide the user with a notification screen to undo his changes or overwrite the current values ​​in the database.

This is a common use case for legacy state exceptions. My question is related to this use case. What is the best strategy if the user decides to overwrite the current database row with his changes? I went over to the hibernation reference guide and other websites, but all that is mentioned is that you need to catch yourself as old and then process the data overwrites programmatically. I am wondering if hibernate has some utilities to simplify this strategy, the simplest thing I can get if a user decides to overwrite his data, extract the latest version of the object from the database, and then copy all the changed fields to this object and then save the changed object back to the database. But I can’t stop wondering if there is a more elegant solution.

+5
source share
2 answers

I do not think that Hibernate will try to help you with this, because the requirements in this area are likely to be very complex and custom-made.

I assume that if a user had to save an object that was changed by another user at the same time, you most likely would not want to just load the object and copy all the changed fields and discard all other user changes. What happens if both users change the same field? You want to present two versions to the user and ask them to decide which version is correct. A bit like merging changes in a version control system.

, , , .

+1

, . , . merge() . , , ​​ . , .

0

All Articles