EntityManager合并抛出EntityExists异常

时间:2012-03-17 19:29:41

标签: openjpa websphere-6.1

在我在WAS 6.1上部署的EJB项目中,当我尝试调用EntityManamger.merge(obj)时,我得到了这个异常:

Exception data: <openjpa-1.0.4-SNAPSHOT-r420667:955062 nonfatal store error> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "com.data.entity.SomeEntity@23aa23aa".  If this is a new instance, make sure any versino and/or auto-generated primary key fields are null/default when persisting.
FailedObject: com.bmo.ctp.data.entity.attribute.RoleResServiceAttribute@23aa23aa
    at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2368)
    at org.apache.openjpa.kernel.AttachStrategy.persist(AttachStrategy.java:94)
    at org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:95)
    at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
    at org.apache.openjpa.kernel.AttachStrategy.attachCollection(AttachStrategy.java:333)
    at org.apache.openjpa.kernel.AttachStrategy.replaceCollection(AttachStrategy.java:301)
    at org.apache.openjpa.kernel.AttachStrategy.attachField(AttachStrategy.java:220)
    at org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:157)
    at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
    at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101)
    at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3161)
    at org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1142)
    at org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:665)
    at com.ibm.ws.jpa.management.JPAExEmInvocation.merge(JPAExEmInvocation.java:335)
    at com.ibm.ws.jpa.management.JPAEntityManager.merge(JPAEntityManager.java:123)

我没有JPAEntityManager.java的来源;但我认为merge()操作不能抛出EntityExistsException,因为merge首先会检查实体是否存在。如果没有,请插入它。如果是,请更新它。 这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

请检查您的实体经理如何检查实体的存在。它是否使用缓存?或者它是否使用数据库查询。如果存在检查是通过缓存完成的,则通常会发生此问题。

如果您使用的是eclipselink JPA,则此配置将在orm.xml中维护

谢谢, 阿南德