我正在使用 JBoss 7.1.1 和它附带的 Hibernate 的默认实现 (4.0.1)。我有一个消息驱动的 bean,它在同一个事务中创建一个实体并使用实体管理器将其持久化。之后(仍然是同一个事务)我找到了新创建的实体并尝试使用实体管理器用 PESSIMISTIC_WRITE 锁定它,但我得到了一个 OptimisticLockException。它的根源如下:
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [some.package.name.EntityName#aaa1a1a0-d568-11e1-9f99-d5a00a0a12b6]
at org.hibernate.dialect.lock.PessimisticWriteSelectLockingStrategy.lock(PessimisticWriteSelectLockingStrategy.java:95)
at org.hibernate.persister.entity.AbstractEntityPersister.lock(AbstractEntityPersister.java:1785)
at org.hibernate.event.internal.AbstractLockUpgradeEventListener.upgradeLock(AbstractLockUpgradeEventListener.java:99)
at org.hibernate.event.internal.DefaultLockEventListener.onLock(DefaultLockEventListener.java:85)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:693)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:686)
at org.hibernate.internal.SessionImpl.access$1100(SessionImpl.java:160)
at org.hibernate.internal.SessionImpl$LockRequestImpl.lock(SessionImpl.java:2164)
at org.hibernate.ejb.AbstractEntityManagerImpl.lock(AbstractEntityManagerImpl.java:1093)
... 202 more
有什么想法为什么我无法查找新创建的实体?另外,如何使它在创建后立即可供搜索?使用 EM 的合并方法似乎没有帮助......