Q:

EntityManager merge throwing EntityExists exception

In my EJB project deployed on WAS 6.1, when I try to call EntityManamger.merge(obj), I get this exception: 
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)

I do not have the source for JPAEntityManager.java; but I thought the EntityExistsException cannot be thrown by  the merge() operation since merge will first do a check to see if entity exists. If not, insert it. If yes, update it.
What is going wrong here?

A:

Please check how your entitymanager checks the existence of an entity. Is it using Cache? or is it using a DB Query. This issue generally occurs, if the existence check is done through cache.
if you are using eclipselink JPA, this configuration is maintained in orm.xml
Thanks,
Anand

