title: Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails
body: <p>I am working on a basic example to test <code>cascade delete</code> operation but I am getting exception.</p>
  
  <p>I have below entities:</p>
  
  <p><strong>Employee.java</strong></p>
  
  <pre><code>@Entity
  public class Employee {
      @Id
      @Column(name = "EMP_ID")
      private long id;
  
      private String name;
  
      @OneToMany(mappedBy = "employee")
      @Cascade(value = { CascadeType.REMOVE, CascadeType.SAVE_UPDATE })
      private List&lt;EmpDetails&gt; details = new ArrayList&lt;EmpDetails&gt;();
  
  }
  </code></pre>
  
  <p><strong>EmpDetails.java</strong></p>
  
  <pre><code>@Entity
  public class EmpDetails {
      @Id
      private long id;
      private int info;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "EMP_ID")
      private Employee employee;
  }
  </code></pre>
  
  <p>Now I have records in databse with employee id as 10 and corresponding records in employee details table.</p>
  
  <p>Now when I run below query:</p>
  
  <pre><code>    session.beginTransaction();
  
      session.delete(new Employee(10)); // here 10 is the ID of the employee
  
      session.getTransaction().commit();
      session.close();
  </code></pre>
  
  <p>I was thinking hibernate will delete the employee record and the corresponding employee details records as I have set the cascade type to remove. But I am getting exception as :</p>
  
  <blockquote>
    <p>Caused by:
    com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:
    Cannot delete or update a parent row: a foreign key constraint fails</p>
  </blockquote>
  
  <p>Can someone please help me how to test the cascade delete option here?</p>