title: Hibernate Error: a different object with the same identifier value was already associated with the session
body: <p>I essentially have some objects in this configuration (the real data model is a bit more complex):</p>
  
  <ul>
  <li>A has a many-to-many relationship with B. (B has <code>inverse="true"</code>)</li>
  <li>B has a many-to-one relationship with C. (I have <code>cascade</code> set to <code>"save-update"</code>)</li>
  <li>C is a kind of type/category table.</li>
  </ul>
  
  <p>Also, I should probably mention that the primary keys are generated by the database on save.</p>
  
  <p>With my data, I sometimes run into problems where A has a set of different B objects, and these B objects refer to the same C object.</p>
  
  <p>When I call <code>session.saveOrUpdate(myAObject)</code>, I get a hibernate error saying: <code>"a different object with the same identifier value was already associated with the session: C"</code>. I know that hibernate can't insert/update/delete the same object twice in the same session, but is there some way around this? This doesn't seem like it would be that uncommon of a situation.</p>
  
  <p>During my research of this problem, I have seen folks suggest the use of <code>session.merge()</code>, but when I do that, any "conflicting" objects get inserted into the database as blank objects with all values set to null. Clearly that isn't what we want.</p>
  
  <p>[Edit] Another thing I forgot to mention is that (for architectural reasons beyond my control), each read or write needs to be done in a separate session.</p>