title: Hibernate orphan removal - transaction isolation level
body: <p>I've got strange problem with hibernate. My DB relation looks like:</p>
  
  <pre><code>Parent 
      @OneToMany(mappedBy = "parent", orphanRemoval = true)
      @Cascade({CascadeType.ALL})
      List&lt;Child1&gt; children1
  
  Child1
      @ManyToOne
      @JoinColumn(name = "PARENT_ID")
      Parent parent;
  
      @OneToMany(mappedBy = "child1", orphanRemoval = true)
      @Cascade({CascadeType.ALL})
      List&lt;Child2&gt; children2;
  
  Child2
      @ManyToOne
      @JoinColumn(name = "CHILD1_ID")
      Child1 child1;
  </code></pre>
  
  <p>Both <code>children1</code> and <code>children2</code> list have ALWAYS size 1. 
  We have scenario when we clear <code>Parent.children1</code> list (to delete rows from database) and add new children to this list (to insert new rows to database). Don't ask why we want to delete and insert new row instead of update existing, let's assume that this is necessary. This scenario looks like:</p>
  
  <blockquote>
    <ol>
    <li>Clear <code>Parent.children1</code> list</li>
    <li>Create new <code>Child2</code> </li>
    <li>Create new <code>Child1</code> and add object created in point 2 (<code>Child2</code>) to <code>children2</code> list</li>
    <li>Add object created in point 4 (<code>Child1</code>) to <code>children1</code> list</li>
    <li>Save <code>Parent</code>.</li>
    </ol>
  </blockquote>
  
  <p>This is done in one transaction. When during this transaction I try to find Parents in database (using hibernate, in different transaction) I see that sometimes <code>children2</code> list is empty. What is wrong? Before saving parent to database we always add one <code>Child2</code> object to <code>children2</code> list, so there shouldn't be situation when <code>children2</code> list is empty.</p>