title: How do I merge two detached entities that are equals() but not identical?
body: <p>I've a entity setup that boils down to something like that:</p>
  
  <pre><code>Person (N:1) -&gt; Job (1:N) -&gt; Company
  </code></pre>
  
  <p>So a person has a <code>java.util.Set</code> of <code>Job</code> instances. The set is defined like this:</p>
  
  <pre><code>@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
  private Set&lt;Job&gt; jobs = new LinkedHashSet&lt;&gt;();
  </code></pre>
  
  <p>And the company looks like this:</p>
  
  <pre><code>@Entity
  public class Company {
      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      @Column(nullable = false, unique = true)
      private String name;
      @OneToMany
      private List&lt;Stelle&gt; jobs = new ArrayList&lt;Stelle&gt;();
  
      public String getDisplayText() { return name; }
      @Override
      public int hashCode() {
          int result = 1;
          result = 31 * result
              + ((getDisplayText() == null) ? 0 : getDisplayText().hashCode());
          return 31 * result + ((getId() == null) ? 0 : getId().hashCode());
      }
      @Override
      public boolean equals(Object obj) {
          if (!(obj instanceof Company))
             return false;
          return !(getId() == null &amp;&amp; ((Company) obj).getId() != null)
               || getId() == null
               || (getId().equals(((Company) obj).getId());
      }
      // getters + setters
  }
  </code></pre>
  
  <p>I create and attach jobs to a person instance in my frontend. The company is selected and attached to the new <code>Job</code> entity with a <code>javax.faces.Converter</code>, where it is fetched from the persistence context via its id.</p>
  
  <p>When I call <code>entityManager.merge(person)</code> on a person that has two <code>Job</code>s with the same <code>Company</code>, I get this exception: </p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: Error occurred while storing 
    entity [example]. An entity copy [com.company.entity.Company#1] was already
    assigned to a different entity [example].
  at org.hibernate.event.internal.EventCache.put(EventCache.java:192)     
  at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:287)
  at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:153)
  at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:851)
  at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:833)   
  at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
  [...]
  </code></pre>
  
  <p>My problem <em>seems</em> to be that <code>DefaultMergeEventListener.java:113</code> (Hibernate-4.3.0.CR1) says:</p>
  
  <pre><code>if ( copyCache.containsKey( entity ) &amp;&amp;
          ( copyCache.isOperatedOn( entity ) ) ) {
      LOG.trace( "Already in merge process" );
      event.setResult( entity );
  }
  else { /* fetch entity and put it in copyCache */ }
  </code></pre>
  
  <p><code>copyCache</code> is a <code>java.util.IdentityHashMap</code>, so my two detached entities are <em>not</em> considered equal for this cache, even if they are for according to their <code>equals()</code> method.</p>
  
  <p>My question is simple: can I somehow keep using the flow described above or what do I have to alter to get Hibernate to merge this entity graph?</p>
  
  <p><em>Edited to add:</em> I'm aware of <a href="https://stackoverflow.com/q/17634230/785663">Hibernate: An entity copy was already assigned to a different entity</a> and <a href="https://stackoverflow.com/q/18147031/785663">IllegalStateException: Error occurred while storing entity &lt;entity&gt; An entity copy &lt;entity&gt; was already assigned to a different entity &lt;entity_copy&gt;</a> describing similar problems.</p>