title: javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session
body: <p>I have two entities (A &amp; B). A has one to many relation with the entity B. Primary key of the entity A is auto increment where as the primary key of entity B is composite (consisting of PK of entity A as a foreign key and one auto incremented id).</p>
  
  <p>Note: I am using EJB 3.1 with MySQL, Jboss7</p>
  
  <p>I populate the entity A from my business objects and create it in the following way.</p>
  
  <pre><code>A a = new A();
  Set&lt;B&gt; bEntitiesList = new HashSet&lt;B&gt;();
  B b = new B();
  b.setCompositeKey(new compositeKey()); // I am not setting any value in the 
  // key and it will have null values for idA and idB.
  // idB is null because it will be auto incremented in this table by MySQL
  bEntitiesList.put(b);
  a.setBEntities(bEntitiesList);
  entityManager.persist(a);
  </code></pre>
  
  <p>Whenever I pass one object of Entity B in the bEntities list then the program works as expected all the fields in the database are populated correctly but when i add more than one object entity B in the bEntities list then I get the exception "javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session". I am not sure how to work around this one. The message clearly says that the hibernate session has found duplicate entries (which is due to the fact that compositekey has overriden methods generated by default by MyEclipse which generates the same hashcode but I am not sure this is causing the problem). Kindly tell me how can i solve this. Is there a need to override the equals and hashcode methods in the compositekey class? Thanks in advance.</p>
  
  <pre><code>public class A
  {
      private Integer idA;
      private Set&lt;B&gt; bEntities = new HashSet&lt;B&gt;(0);
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "A", unique = true, nullable = false)
      public Integer getIdA() {
        return this.idA;
      }
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "A")
      public Set&lt;B&gt; getBEntities() {
        return this.bEntities;
      }
  }
  
  public class B
  {
      private compositeKey key;
      private A entityA;
      @EmbeddedId
      @AttributeOverrides({
      @AttributeOverride(name="idB",column=@Column(name="idB",nullable=false))})
      public compositeKey  getKey() {
         return this.key;
      }
      public void setKey(compositeKey key) {
        this.key = key;
      }
      @ManyToOne(fetch = FetchType.LAZY)
      @MapsId("providerReferenceNo")
      @JoinColumn(name = "idA", nullable = false, insertable = false, updatable = false)
      public A getEntityA() {
        return this.entityA;
      }
  }
  
  
  @Embeddable
  public class compositeKey {
  private Integer idA;
  private Integer idB;
  
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "idB", nullable = false)
  public Integer getIdB() {
    return this.idB;
  }
  @Column(name = "idA", nullable = false)
  public Integer getIdA() {
    return this.idA;
  }
  </code></pre>