title: Hibernate: An entity copy was already assigned to a different entity
body: <p>HiAll,</p>
  
  <p>i try currently to understand, what is wrong in my coding and entity model 
  I use the hibernate 4.2 to for CRUD operation with just Generic DAO pattern and annotated entities with cascading ALL and orphanRemoval= true. 
  I have the <em>DayPlan</em> entity with OneToMany relationship to <em>DayPlanElement</em> weak entity. The  <em>DayPlanElement</em> are saved into java.util.Set. Each <em>DayPlanElement</em> has a property "order".</p>
  
  <p>The <em>DayPlan</em> has also  OneToMany relationship to an entity <em>Person</em>. The entity <em>Person</em> saved into java.util.<strong>List</strong>. </p>
  
  <p>The <em>DayPlanElement</em> has OneToMany relationship to <em>DayPlanElementEntry</em> weak entity. java.util.Set used for save.
  The <em>Person</em> entity also OneToMany relationship to <em>DayPlanElementEntry</em> weak entity.
  java.util.Set used for save.
  The <em>DayPlan</em>, <em>DayPlanElement</em> and  <em>Person</em> entities have the ID, managed by my application, as String. 
  The <em>DayPlanElementEntry</em> weak entity has a composite Id: DayPlanElementEntryId annotated with EmbeddedId, contains the parentPersonId and dayPlanElementId. It</p>
  
  <p>In other words imagine, that exists a table, represents the day plan. The columns are the hours from 0 up to 24. The rows are the persons, that must proceed the operations from day plan. Each column would be DayPlanElement entity. Each row would be Person entity. And each cell would be DayPlanElementEntry entity.</p>
  
  <p>If i just add to table new elements (also Persons) and remove its (also remove its from List and then call the DayPlanDAO.merge(dayPlan) - i hope on cascading and orphanRemoval) - i have no problem. </p>
  
  <p>Only if i try to reorder the given Persons (just removing operations within java.util.List) and the call DayPlanDAO.merge(dayPlan) - the following excepion will be thrown:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: 
  Error occurred while storing entity 
  [DayPlanElementEntry [getDayPlanMode()=NONE, getCompositeId()=DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6], hashCode=-2039940039]]. 
  
  An entity copy 
  [org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6]] 
  
  was already assigned to a different entity 
  
  [org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6]].
  
         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)
  </code></pre>
  
  <hr>
  
  <p>I debugeg the EventCache, seaching for problem solutions, just check implemented equals() and hashCode() method - without success:-((
  Notice, i use for equals() method the Id. For instance the composite id of DayPlanElementEntry. That id is used by hashCode(). I just not understand the error message, thrice times will be output the same properties - the ids!</p>
  
  <p>I found the <a href="https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/hibernate/event/internal/EventCacheTest.java">test class</a> for event cache, but now i don't understand the following use case test:</p>
  
  <pre><code>    @Test
  public void testCopyAssociatedWith2ExistingEntities() {
  session.getTransaction().begin();
  Simple entity1 = new Simple( 1 );
  session.persist( entity1 );
  Simple copy1 = new Simple( 1 );
  cache.put(entity1, copy1);
  Simple entity2 = new Simple( 2 );
  session.persist( entity2 );
  Simple copy2 = new Simple( 2 );
  cache.put( entity2, copy2 );
  session.flush();
  
  try {
  cache.put( entity1, copy2 );
  fail( "should have thrown IllegalStateException");
  }
  catch( IllegalStateException ex ) {
  // expected
  assertTrue( ex.getMessage().startsWith( "Error occurred while storing entity [org.hibernate.event.internal.EventCacheTest$Simple#1]." ) );
  }
  session.getTransaction().rollback();
  }
  </code></pre>
  
  <p>....</p>
  
  <pre><code>    @Entity
  private static class Simple {
  @Id
  private int value;
  
  public Simple(int value) {
  this.value = value;
  }
  
  public int getValue() {
  return value;
  }
  
  public void setValue(int value) {
  this.value = value;
  }
  }
  </code></pre>
  
  <hr>
  
  <p>We have the Simple entity entity1 with <strong>Id=1</strong>, Then we persist it. Then we create the Simple entity copy1 and put into event cache it as value. As key the persisted entity*<em>1</em>* used.
  Then we create a Simple entity*<em>2</em>* with <strong>Id=2</strong> , persist it, and then create the copy*<em>2</em>* entity with id 2 and put it as value into event cache. As key the persisted <strong>entity2</strong> used.
   Why such situation is wrong and why is expected, that IllegalStateException must be thrown??</p>
  
  <p>Thanks for your help!</p>