Detached entity cannot be merged via cascade merge in JBoss EAP 6

Solution Verified - Updated -

Issue

  • Migrating Hibernate 3 application to Hibernate 4
  • EntityA has two CascadeType.MERGE associations to EntityB: relatedB1 and relatedB2
  • An instance of EntityB is instantiated, persisted, detached then associated with EntityA via relatedB1
  • The persisted instance of EntityB is loaded by a query and associated with EntityA via relatedB2
  • When EntityA is merged to the data store, an exception like the following is raised:
java.lang.IllegalStateException: Error occurred while storing entity [test.EntityB@20]. An entity copy [test.EntityB#1] was already assigned to a different entity [test.EntityB@20].
    at org.hibernate.event.internal.EventCache.put(EventCache.java:192)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
    ...
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:902)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889)
    ...

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6
  • Hibernate 4.1.3 and later

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content