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 of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.