1

We keep user object in session. When we update it first time ( using entityManager.merge( ent )) it works fine; but second time I get following exception:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.kids.domain.Child#22]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:492)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:256)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:86)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:781)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:766)

I am using spring with hibernate & jpa. Spring is injecting EntityManager in DAO. I do understand that if I would do a fresh find() everytime then make all the changes on that object and then merge().. it will work fine.. but in my case I cannot do that.

I also checked that the hashcode of EntityManager used is same both time; i.e same EntityManager instance is getting used both time then why it gives this exception when trying to update a record second time

Any help would be highly appreciated.

4

2 回答 2

2

我尝试了很多东西,但没有任何效果。即使在互联网上也没有解决方案。最后,当我从我的实体中删除版本时(即删除 hibernate 提供的 OptimisticLockException 支持).. 一切都开始正常工作。可能,我会尝试自己处理 OLE。

于 2012-07-04T08:00:39.630 回答
0

当您在当前事务中调用 merge 方法时,hibernate 会将给定对象的状态复制到具有相同标识符的持久对象上,并返回新的可管理实体。您需要在后续操作中处理合并返回的可管理实体,因为该实体具有比 DB 中的新版本。

于 2012-07-03T13:06:44.030 回答