title: Hibernate "deleted entity passed to persist" issue with Cascade.DELETE_ORPHAN and parent/child relationship
body: <p>I have the following self-table mapping:</p>
  
  <pre><code>public class Node implements {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  
      ...
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "IDFATHER", referencedColumnName = "ID")
  private Node father;
  
  @OneToMany(mappedBy = "father", fetch = FetchType.EAGER)
  @Fetch(FetchMode.JOIN)
  @Cascade(value =
  {
          CascadeType.ALL,
          CascadeType.DELETE_ORPHAN
  })
  private List&lt;Node&gt; children;
  </code></pre>
  
  <p>Basically it's a classical parent/child node tree using the same table, with the column IDFATHER pointing to the id of the father node.</p>
  
  <p>I have implemented some basic operations on the tree:</p>
  
  <ol>
  <li>Bulk delete : delete a selected node + all its children  </li>
  <li>Single delete : delete only the selected node, re-attach all its children to its parent</li>
  <li>ect...</li>
  </ol>
  
  <p>To implement operation 2) Single delete:</p>
  
  <pre><code>// father of the node to be deleted
  Node father = deletedNode.getFather();
  
  if (deletedNode.getChildCount() != 0)
  {
      List&lt;eNode&gt; tempChildren = new ArrayList&lt;Node&gt;();
  
      // put all children of deleted node in a temp list because the
      // new FOR loop doesn't allow concurrent modification while looping  
      for (Node child : deletedNode.getChildren())
      {
              tempChildren.add(child);
      }
  
      for (Node child : tempChildren)
      {
           // re-attach first all the children to the father
          father.getChildren().add(child);
          child.setFather(father);
  
           // remove all the children from the deleted node list
          deletedNode.getChildren().remove(child);
  
           // remove the deleted node from the father children' list
          father.getChildren().remove(deletedNode);
      }
  }
  this.nodeDAO.flush();
  </code></pre>
  
  <p>I got the exception </p>
  
  <blockquote>
    <p>javax.persistence.EntityNotFoundException: deleted entity passed to
    persist</p>
  </blockquote>
  
  <p>As far I understand, according to the official documentation, when you delete an entity, with <code>Cascade.ALL</code>, the deletion is cascaded to its children. However in this specific case, all the children are re-attached to the father so they should not be deleted...</p>
  
  <p>When I remove the <code>Cascade.DELETE_ORPHAN</code>, it works. Logically by re-attaching the children to the father, they are no longer orphan so the <code>Cascade.DELETE_ORPHAN</code> should'nt really matter. </p>
  
  <p>Any clue on this issue?</p>