Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) 异常原因及处理



错误描述

报错内容

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.gp.prof.domain.TitleApplication#88234]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:303)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:903)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:887)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891)
    at com.gp.common.dao.hibernate4.BaseHibernateDao.merge(BaseHibernateDao.java:106)

遇到该异常说明操作对象时应用了乐观锁机制或定义POJO时定义了version字段

当前操作version版本与数据库中version版本不一致

解决方法

1.在提交数据时,把version也一并提交上来

2.在数据库中把version的值查出来,赋给实体对象,再执行更新操作

注:每次执行更新才做之后,version的值都会改变,所以需要再次执行更新操作,必须先把version的值查出来,赋最新的version值,再执行更新操作。

声明:Wayen|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) 异常原因及处理


-当你感到最无助困难的时候,那就是离成功最近的时候。-Martin Frohm