title: merge with hibernate causing error with dirty objects
body: <p>I'm using Hibernate with Spring in my application. I have been consistently using detached objects and reattaching them whenever I need to make calls to the database. I'm using hibernate template to take care of managing the connections for me.</p>
  
  <p>My data structure is as follows:</p>
  
  <pre><code>classA: 
     &lt;one-to-many name="classB" inverse="true" cascade="all,delete-orphan" lazy="true"&gt;
  
  classB:
     &lt;one-to-many name="classC" inverse="true" cascade="all,delete-orphan" lazy="false" fetch="subselect"&gt;
  </code></pre>
  
  <p>When I make the following call, with the potential that some of the items in classB or classC have changed, I get an error:</p>
  
  <pre><code>classA a = (classA)hibernateTemplate.merge(newClassA);
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>[11/10/08 12:08:39:126 EST] 00000024 SystemOut     O 2008-11-10 12:08:38,876  -  - ca.thedominion.cfa.persistence.hibernate.ClassADAOImpl  :updateClassA Technical Exception occurred: 
  org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [ca...classC#715590]; nested exception is org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [ca...classC#715590]
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:639)
      at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
      at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
      at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
      at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:820)
  </code></pre>
  
  <p>Can someone explain to me why this is generating an error? As I understand it, merge should determine which, if any, objects in the tree changed, and update/delete/insert those, and not attempt to delete and re-insert any others. But this seems to be trying to delete all the elements of type classC and re-insert them.</p>