title: Hibernate: different object with the same identifier value was already associated with the session
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1074081/hibernate-error-org-hibernate-nonuniqueobjectexception-a-different-object-with">Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session</a>  </p>
  </blockquote>
  
  
  
  <p>when I use the DAO.update(userbean), <code>session.SaveOrUpdate(e);</code>throw the Exception:different object with the same identifier value was already associated with the session</p>
  
  <p>the function is like next:</p>
  
  <pre><code>    public E save(E e) {
      Session session = null;
      try {
          session = sessionFactory.openSession();
          log.debug("session="+session.hashCode()+" save "+e);
          session.SaveOrUpdate(e);  //here throws exception 
          session.flush();
      }
      catch (Exception e1) {
          log.err("Cannot open hibernate session "+ e1.getMessage()+" cause : "+e1.getCause());
          e1.printStackTrace();
      }
      finally { if ( session != null ) session.close(); session = null;}
      return e ;
  }
  </code></pre>
  
  <p>the userbean is an instance of class UserBean</p>
  
  <pre><code>public class UserBean{
     private List&lt;GroupBean&gt; groups = new ArrayList&lt;GroupBean&gt; ();
     private List&lt;RoleBean&gt; roles = new ArrayList&lt;RoleBean&gt; ();
  }
  
  public class GroupBean{
  private List&lt;RoleBean&gt; roles = new ArrayList&lt;RoleBean&gt; ();
  }
  </code></pre>
  
  <p>every groupbean has a list of roles, which are not changed.</p>
  
  <p>in database, group and role is many-to-many mapping, </p>
  
  <p>for example, </p>
  
  <p>we have a groupbean#1, it's roles:  rolebean#1, rolebean#2;</p>
  
  <p>groupbean#2, which roles are rolebean#1.</p>
  
  <p>now I create a new userbean#1, it's groups is groupbean#1
  and if I want to add the rolebean#1 to userbean#1, it will throws the exception like the title descript</p>
  
  <p>I look the server.log, and find that when I user DAO.save, the saveOrUpdate order is:</p>
  
  <pre><code>userbean#1
  |---|-----------***userbean.groups
  |     |     groupbean#1
  |     |         groupbean.roles
  |     |             rolebean#1  # save relebean#1 the first time
  |     |             ---done rolebean#1
  |     |         ------done all rolebeans of group.roles
  |     |     ---done groupbean#1
  |     |-----------done all groupbeans of userbean.groups
  |---|-----------***userbean.roles
       |      rolebean#1          # save rolebean#1 the second time, and throws exception here!
       |      ----done rolebean#1
       |      .....
       |-----------done all rolebeans of userbean.roles
  </code></pre>
  
  <p>the cause of the exception is rolebean#1 has been saved twice in a session, and their identity is the same.</p>
  
  <p>In the function save(E e), If I use</p>
  
  <pre><code>session.merge(e);
  </code></pre>
  
  <p>replace</p>
  
  <pre><code>session.SaveOrUpdate(e);
  </code></pre>
  
  <p>will not throw exception, but the rolebean#1 is not assocaited to userbean#1</p>
  
  <p>anyone can give some suggestions about this? </p>