title: hibernate insert to a collection causes a delete then all the items in the collection to be inserted again
body: <p>I have a many to may relationship CohortGroup and Employee. Any time I insert an Employee into the CohortGroup hibernate deletes the group from the resolution table  and inserts all the members again, plus the new one. Why not just add the new one?</p>
  
  <p>The annotation in the Group:</p>
  
  <pre><code>@ManyToMany(cascade = { PERSIST, MERGE, REFRESH })
  @JoinTable(name="MYSITE_RES_COHORT_GROUP_STAFF",
  joinColumns={@JoinColumn(name="COHORT_GROUPID")},
  inverseJoinColumns={@JoinColumn(name="USERID")})
  public List&lt;Employee&gt; getMembers(){
    return members;
  }
  </code></pre>
  
  <p>The other side in the Employee</p>
  
  <pre><code>@ManyToMany(mappedBy="members",cascade = { PERSIST, MERGE, REFRESH } )
  public List&lt;CohortGroup&gt; getMemberGroups(){
    return memberGroups;
  }
  </code></pre>
  
  <p>Code snipit</p>
  
  <pre><code>Employee emp = edao.findByID(cohortId);
  CohortGroup group = cgdao.findByID(Long.decode(groupId));
  group.getMembers().add(emp);
  cgdao.persist(group);
  </code></pre>
  
  <p>below is the sql reported in the log</p>
  
  <pre><code>delete from swas.MYSITE_RES_COHORT_GROUP_STAFF where COHORT_GROUPID=?
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
  </code></pre>
  
  <p>This seams really inefficient and is causing some issues. If sevral requests are made to add an employee to the group then some get over written.</p>
  
  <p>Seams like equals and hashCode might be a reason for this. Below are the implementation for these methods. Any red flags?</p>
  
  <p>CohortGroup</p>
  
  <pre><code>    @Override
  public int hashCode() {
      final int prime = 31;
      int result = getName().hashCode();
      result = prime * result + ((emp == null) ? 0 : emp.hashCode());
      return result;
  }
  @Override
  public boolean equals(Object obj) {
      if (this == obj) {return true;}
      if (!(obj instanceof CohortGroup)) {return false;}
      CohortGroup other = (CohortGroup) obj;
      if(!getName().equals(other.getName())){return false;}
      if (emp == null &amp;&amp; other.getOwner() != null) {
          return false;
      } else if (!emp.equals(other.getOwner())) {
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>Employee </p>
  
  <pre><code>       @Override
  public boolean equals(Object obj) {
      if (this == obj) {return true;}
      if (obj == null) {return false;}
      if (!(obj instanceof Employee)) {return false;}
      Employee other = (Employee) obj;
      if (EMPLID == null &amp;&amp; other.getEMPLID() != null) {
          return false;
      } else if (!EMPLID.equals(other.getEMPLID())) {
          return false;
      }
      return true;
  }
  
     @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((EMPLID == null) ? 0 : EMPLID.hashCode());
      return result;
  }
  </code></pre>
  
  <p>I have added an addMember method to the CohortGroup that adds to both sides of the relationship:</p>
  
  <pre><code>    public void addMember(Employee emp){
      this.getMembers().add(emp);
      emp.getMemberGroups().add(this);
  
  }
  </code></pre>
  
  <p>Continued thanks to all that are helping.</p>