更新对象时发生HibernateOptimisticLockingFailureException [英] HibernateOptimisticLockingFailureException while updating an object

查看:93
本文介绍了更新对象时发生HibernateOptimisticLockingFailureException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新对象时遇到异常.

I am getting following exception while updating an object.

HibernateOptimisticLockingFailureException:类[User]的对象标识符为[25614]:乐观锁定失败;嵌套异常是org.hibernate.StaleObjectStateStateException:行已更新或删除通过另一笔交易(或未保存的值映射不正确):[User#25614]

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 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?

推荐答案

问题的答案是:在第一个事务中更改密码时,用户实体的version字段将更新,但是您保留了过时的版本您在HTTP会话中的用户对象,并尝试在第二次交易中使用此过时的版本更新用户.

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.

只需确保每次更改密码后都刷新在HTTP会话中保留的用户对象.

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);

这篇关于更新对象时发生HibernateOptimisticLockingFailureException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆