title: OneToMany ManyToOne removing child object with Hibernate
body: <p>I'm using Spring Boot and Hibernate.
  Lets assume we have two entities:</p>
  
  <pre><code>@Entity
  public class A{
      @OneToMany(mappedBy = "objectA",fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval=true)
      private Set&lt;B&gt; objectSet = new HashSet&lt;&gt;();
  }
  @Entity
  public class B{
      @ManyToOne
      private A objectA;
  }
  </code></pre>
  
  <p>And we have two transacional methods;</p>
  
  <pre><code>deleteB_X(int idB){
      entityManager.remove(entityManager.find(idB,B.class));
  }
  deleteB_Y(int idB){
      B obj=entityManager.find(idB,B.class);
      obj.getObjectA().getObjectSet().remove(obj);
  }
  </code></pre>
  
  <p>What I understand (correct me if I'm wrong):</p>
  
  <ol>
  <li>We have <code>orphanRemoval=true</code> so <code>deleteB_Y(int)</code> will work.</li>
  <li>By setting <code>mappedBy</code> argument we say that class A is "the owning
  site" of relation.</li>
  <li>CascadeType is used when we persist/update/merge/remove class A (then it invokes persist/update/merge/remove on child property <code>objectSet</code>). I think we can say that it protects me from situation where I end up with object of B and no object of A class (unless we manually add some B objs).</li>
  </ol>
  
  <p>From what I understand CascadeType should not interfare with orphanRemoval, because CascadeType takes care of things where we do 'some stuff' with A's objects (and then recursively do it to B's objects). And here is something that I don't understand at all.
  Why <code>deleteB_x(int)</code> doesn't work and why if we remove CascadeType it starts working? I feel like that <code>deleteB_X(int)</code> is much cleaner solution to removing object B from the DB than <code>deleteB_Y(int)</code>, but sadly it won't work since it colides with CascadeType.</p>
  
  <p>EDIT1.
  Method <code>deleteB_X(int)</code> just doesn't remove object from DB, if we remove <code>cascade = CascadeType.ALL</code> evertyhing works just fine. Why?</p>