title: Hibernate: Cannot Delete Object - foreign key constraint - CascadeType.REMOVE
body: <p>I have two persistable entities Obj1 and Obj2. Obj2 contains a reference to Obj like below. There is no reference (at code level - Java) to Obj2 in Obj1.</p>
  
  <pre><code>public class Obj2{
  ....
  @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE}, optional = true)
  private Obj1 obj1;
  </code></pre>
  
  <p>I updated Ojbj 1 with @OneToMany as suggested:</p>
  
  <pre><code>public class Obj1{
  
  ....
  
      @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE,CascadeType.ALL},orphanRemoval = true)
      private List&lt;Obj2&gt; obj2;
  </code></pre>
  
  <p>When I try to delete Obj1: </p>
  
  <pre><code>em.remove(obj1Instance)
  </code></pre>
  
  <p>It fails due to Foreigh Key constaint</p>
  
  <pre><code>00:35:23,109 WARN  [com.arjuna.ats.arjuna] (http--127.0.0.1-8080-1) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple&lt; 0:ffff7f000101:-7f20c644:5369716f:26, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@dcedf7 &gt;: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`my_db`.`obj2`, CONSTRAINT `FKC4783505BB5D6339` FOREIGN KEY (`obj1_pk`) REFERENCES `obj1` (`pk`))
  ...
  ... 94 more
  Caused by: org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`my_db`.`obj2`, CONSTRAINT `FKC4783505BB5D6339` FOREIGN KEY (`obj1_pk`) REFERENCES `obj1` (`pk`))
  
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`my_db`.`obj2`, CONSTRAINT `FKC4783505BB5D6339` FOREIGN KEY (`obj1_pk`) REFERENCES `obj1` (`pk`))
  </code></pre>
  
  <p>I had (wrongly it seems!) asumed that CascadeType.REMOVE would take care of this? How should I handle this delete?</p>
  
  <p>/T</p>