开发者

HibernateOptimisticLockingFailureException while updating an object

开发者 https://www.devze.com 2023-04-08 08:39 出处:网络
I am getting following exception while updating an object. HibernateOptimisticLockingFailureException: Object of class [User]

I am getting following exception while updating an object.

HibernateOptimisticLockingFailureException: Object of class [User] with identifier [25614]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [User#25614]

Situation :- Reason why i am facing this error is that I have a form where users are displayed and I have two buttons there one for updating the password and one for editing the detail of the users. when I click on updating the password then it just query the object and updates its password and keep the object in hibernate session. And then I click on edit button and modify the information and then save it then it gives upper mentioned exception because the object, that I am trying to save, is not hibernate session object but an object with same identifier was queried by hibernate while updating the password. Now I have two objects with same identifier one 开发者_JAVA技巧is in hibernate session and another is not persisted (not detached object) yet. I want to update save the changes from not persisted object into the database but because there is an object with same identifier is in hibernate session so hibernate gives an exception.

How I can merge changes from not persisted objects to persisted one?


The answer is in the question: when changing the password in the first transaction, the version field of the user entity is updated, but you keep an obsolete version of your user object in the HTTP session and try to update the user with this obsolete version in a second transaction.

Just make sure to refresh the user object you keep in the HTTP session each time the password is changed.

You might also manually copy each property of the modified user to the attached user object, but then you wouldn't benefit from optimistic locking anymore.

// first transaction:
User refreshedUser = userService.updateUserPassword(userId, newPassword);
request.getSession().setAttribute("user", refreshedUser);

// ...
// second transaction:
User modifiedUser = (User) request.getSession().getAttribute("user");
modifiedUser = userService.updateUser(modifiedUser);
request.getSession().setAttribute("user", modifiedUser);


I was facing the same issue and found the record I am deleting was having a foreign key associated and it was deleting all related entries from another table and in the second transaction I have to update the record on another table. So this might be also I use case of the "HibernateOptimisticLockingFailureException while updating an object"

0

精彩评论

暂无评论...
验证码 换一张
取 消