title: JPA: Bidirectional association & orphanRemoval - Need to remove association from both sides (parent and child)?
body: <p>So that's a question which it seems there's no official answer to. At least I can't find one.</p>
  
  <p>Situation: If I have a bidirectional One-to-many association and I want orphanRemoval=true to take action, do I now have to remove the association from both sides of the association or is it enough to just "break" the association by deleting the reference (to the opposite) in one of the entities?</p>
  
  <p>Example:</p>
  
  <pre><code>class Parent {
  @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
  List&lt;Child&gt; children;
  
   public void addChild(Child child) {
      child.setParent(this);
      children.add(child);
   }
  
   public void removeChild(Child child){
      //both statements neccessary or just one? Does it matter which one?
      children.remove(child);
      if (child != null) {
          child.setParent(null);
      }
   }
  }
  
  class Child {
   String name;
   @ManyToOne
   @JoinColumn(name="parentID")
   Parent parent;
  
   public void setParent(Parent p) {
    [...]
   }
  
  }
  </code></pre>
  
  <p>I came across this explanation in my book "Pro JPA 2 by Mike Keith" (2nd ed., p. 287):</p>
  
  <blockquote>
    <p>When specified, the orphanRemoval element causes the child entity to be removed when the relationship between the parent and the child is broken. 
    This can be done <strong>either by setting to null the attribute that holds the
    related entity, <em>or</em> additionally in the one-to-many case by removing the child entity from the collection</strong>. The provider is then responsible, at flush or commit time (whichever comes first), for removing the orphaned child entity.
    In a parent-child relationship, the child is dependent upon the existence of the parent. If the parent is removed,
    then by definition the child becomes an orphan and must also be removed.</p>
  </blockquote>
  
  <p>And then there are those posts which do both of them:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/23926548">https://stackoverflow.com/a/23926548</a></li>
  <li><a href="https://stackoverflow.com/a/3071125">https://stackoverflow.com/a/3071125</a></li>
  <li><a href="https://coderanch.com/t/652044/databases/Delete-Bidirectional-Entities#3012875" rel="nofollow noreferrer">https://coderanch.com/t/652044/databases/Delete-Bidirectional-Entities#3012875</a></li>
  </ul>
  
  <p><strong>My questions:</strong></p>
  
  <p>1) What's correct / which one of both are necessary now?</p>
  
  <p>2) What happens if the Child class has an
      @JoinColumn(name="parentID", <strong>nullable=false</strong>) and its parent
      reference will get set to  null as seen above? My guess: the
      orphanRemoval would take place later and is based on the value in
      the database, so not until the Child entity was updated. But that
      update wouldn't happen, because of nullable=false, right?</p>