Reputation: 22
I am new to Hibernate, I could see that Hibernate throws StaleObjectStateException while multiple users trying to persist the complete entity. But, Most of the DB updates I have done using HQL update query. Now, I have added an extra condition to the HQL update queries as 'where version = :currentVersion' to identify no other user updated the particular record. It seems to be working fine. But, problem is that I have many number of queries and I also have to synchronize the version number in my java object as same in DB. Is there any simple way to get 'StaleObjectStateException' on HQL query update during multi user updates?
Upvotes: 0
Views: 102
Reputation: 526
You understood this wrong. Hibernate throws StaleObjectStateException as a wanted behaviour for "while multiple users trying to persist the complete entity". This prevents that the last writer wins and overrides the data from his antecessors. Usually you catch this Exception and show some Error Message to the User like "Someone has changed the Data. Please Retry!". Your HQL query clause is the wrong way and will force you to patch your code even more and more.
Upvotes: 1