title: JPA add new child to child of parent causes dublicated key
body: <p>I guess I don't really understand bidirectional one-to-many relations.</p>
  
  <p>I have a parent, a child-A and child-B class with bidirectional relations between Parent &lt;-> Child-A &lt;-> Child-B. I want to add a child-B to the child-B-list of the child-A, but on every try to merge the parent I get an dublicated key exception. So how do I add a new child-B.</p>
  
  <pre><code>class Parent {
      long id;
  
      @One-To-Many, MappedBy Parent, Cascade All, OrphanRemoval true
      list &lt;child-A&gt; childsA;
  
      ...
  
      setter + getter
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Child-A {
      long id;
  
      @Many-To-One, JoinColums parentId, Cascade PERSIT MERGE REFRESH
      Parent parent;
  
      @One-To-Many, MappedBy Child-A, Cascade All, OrphanRemoval true
      list &lt;child-B&gt; childsB;
  
      ...
      setter + getter
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Child-B {
      long id;
  
      @Many-To-One, JoinColums Child-A-id, Cascade PERSIT MERGE REFRESH
      Child-A child-A;
  
      ...
      setter + getter
  }
  </code></pre>
  
  <p>How do I add a new child-B to child-A and merge the parent to save everything in the db? So far I've tried:</p>
  
  <pre><code>Parent p = entityManager.getParent();
  Child-A ca = p.getChildsA.get(indexOfCa); // the index is known
  Child-B cb = new Child-B ();
  
  ... // fill cb with information
  
  ca.add(cb); // automatically fires cb.setChild-A(ca);
  p.getChildsA.set(index, ca);
  entityManager.merge(p);
  </code></pre>
  
  <p>But this causes a DublicatedKeyException. So what is the best practise to add a child-B object to an already persisted child-A-object from an parent-object?</p>
  
  <p>I also have to say, only merge is possible (no save, saveOrUpdate or persist) and it is not possible to edit the entity-classes. The entities are generated by something like a factory and every change will be overwritten when building the project. And the pom-file is not editable.
  Also this is a Java EE web-application with many different frameworks like primefaces and omnifaces.</p>