Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException with Hibernate 4 and ManyToOne cascading

Tags:

I've got those two classes

MyItem Object:

@Entity public class MyItem implements Serializable {      @Id     private Integer id;     @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})     private Component defaultComponent;     @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})     private Component masterComponent;      //default constructor, getter, setter, equals and hashCode } 

Component Object:

@Entity public class Component implements Serializable {      @Id     private String name;      //again, default constructor, getter, setter, equals and hashCode } 

And I'm tring to persist those with the following code:

public class Test {      public static void main(String[] args) {         Component c1 = new Component();         c1.setName("comp");         Component c2 = new Component();         c2.setName("comp");         System.out.println(c1.equals(c2)); //TRUE          MyItem item = new MyItem();         item.setId(5);         item.setDefaultComponent(c1);         item.setMasterComponent(c2);          ItemDAO itemDAO = new ItemDAO();         itemDAO.merge(item);     } } 

While this works fine with Hibernate 3.6, Hibernate 4.1.3 throws

Exception in thread "main" java.lang.IllegalStateException: An entity copy was already assigned to a different entity.         at org.hibernate.event.internal.EventCache.put(EventCache.java:184)         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.fireMerge(SessionImpl.java:914)         at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:896)         at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)         at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)         at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)         at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)         at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)         at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:423)         at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:213)         at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:282)         at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)         at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)         at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:904)         at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)         at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)         at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:874)         at sandbox.h4bug.Test$GenericDAO.merge(Test.java:79)         at sandbox.h4bug.Test.main(Test.java:25) 

Database backend is h2 (but the same happens with hsqldb or derby). What am I doing wrong?

like image 418
Clayton Louden Avatar asked May 11 '12 11:05

Clayton Louden


1 Answers

I had the same problem, and this is what I found:

The merge method traverses the graph of the object that you want to store, and for each object in this graph it loads it from the database, so it has a pair of (persistent entity, detached entity) for each object in the graph, where detached entity is the entity that is going to be stored, and persistent entity is gotten from the database. (In the method, as well as in the error message the persistent entity is known as 'copy'). Then these pairs are put in two maps, one with the persistent entity as key and the detached entity as value, and one with the detached entity as key and the persistent entity as value.

For each such pair of entites, it checks these maps, to see if the persistent entity maps to the same detached entity as before (if it has already been visited), and vice versa. This problem occurs when you get a pair of entities where doing a get with the persistent entity returns a value, but a get from the other map, with the detached entity returns null, which means that you have already linked the persistent entity with a detached entity with a different hashcode (basically the object identifier if you have not overridden the hashcode-method).

TL;DR, you have multiple objects with different object identifiers/hashcode, but with the same persistence identifier (thus referencing the same persistent entity). This is appearantly no longer allowed in newer versions of Hibernate4 (4.1.3.Final and upwards from what I could tell).

The error message is not very good imo, what it really should say is something like:

A persistent entity has already been assigned to a different detached entity

or

Multiple detached objects corresponding to the same persistent entity

like image 145
Tobb Avatar answered Sep 29 '22 16:09

Tobb