title: a 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/3553200/hibernate-different-object-with-the-same-identifier-value-was-already-associate">Hibernate: different object with the same identifier value was already associated with the session</a>  </p>
  </blockquote>
  
  
  
  <p>I get the following error:</p>
  
  <p>a different object with the same identifier value was already associated with the session</p>
  
  <p>I have two types of objects, Course and RecommendedSchedule which has a set of courses.
  Here are their xml definitions:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name="database.datatypes.Course" table="courses" lazy="false"&gt;
      &lt;id name="id" column="ID"&gt;
           &lt;generator class="assigned"/&gt;
      &lt;/id&gt;
      &lt;property name="name" type="string"/&gt;
      &lt;property name="date"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>and</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name="database.datatypes.RecommendedSchedule" table="recommended_schedules" lazy="false"&gt;
      &lt;id name="id" column="ID"&gt;
           &lt;generator class="increment"/&gt;
      &lt;/id&gt;
      &lt;set name="courses" table="schedules_courses" cascade="save-update" lazy="false"&gt;
          &lt;key column="course_id"/&gt;
          &lt;many-to-many class="database.datatypes.Course"/&gt;
      &lt;/set&gt;
      &lt;property name="semester" type="string"/&gt;
      &lt;property name="path" type="string"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>Basically if i insert two recommended systems with different sets, but using the same Course objects, it works, but if i do the exact same thing but use different Course objects (with the same values) i get the error.</p>
  
  <p>Anyone knows what I'm doing wrong?</p>
  
  <p>Here is an example for a piece of code that fails:</p>
  
  <pre><code>Session s = db.factory.openSession();
      Set&lt;Course&gt; set1 = new HashSet&lt;&gt;();
      Set&lt;Course&gt; set2 = new HashSet&lt;&gt;();
      Course c1 = new Course(104167L, "Algebra A");
      Course c2 = new Course(234114L, "Introduction to CS H and M");
      Course c3 = new Course(104012L, "Calculus 1 T");
      Course c4 = new Course(234145L, "Digital Systems");
      Course c12 = new Course(104167L, "Algebra A");
      Course c22 = new Course(234114L, "Introduction to CS H and M");
      Course c32 = new Course(104012L, "Calculus 1 T");
      Course c42 = new Course(234145L, "Digital Systems");
      set1.add(c1);
      set1.add(c2);
      set1.add(c3);
      set2.add(c12);
      set2.add(c22);
      set2.add(c32);
      set2.add(c42);
      RecommendedSchedule r1 = new RecommendedSchedule(set1, "General 3 years", "1");
      RecommendedSchedule r2 = new RecommendedSchedule(set2, "General 4 years", "1");
      Collection&lt;RecommendedSchedule&gt; col = new ArrayList&lt;RecommendedSchedule&gt;();
      Collection&lt;Course&gt; col2 = new ArrayList&lt;Course&gt;();
      col.add(r1);
      col.add(r2);
      col2.add(c12);
      col2.add(c22);
      col2.add(c32);
      col2.add(c42);
  
      Transaction t = s.beginTransaction();
      s.save(r1);
      s.save(r2);
      t.commit();
      s.close();
  </code></pre>
  
  <p>if i did instead:</p>
  
  <pre><code>set2.add(c1);
  set2.add(c2);
  set2.add(c3);
  set2.add(c4);
  </code></pre>
  
  <p>It would've worked. (This of course isn't the real issue, but a simple example)</p>