title: JPA cascade remove
body: <p>I'm facing a problem while deleting a record that is referenced by another table. </p>
  
  <p>This is a code example (simplest scenario):</p>
  
  <pre><code>@Entity
  @Table(name = "user")
  public class User
  {
      @Id
      @GeneratedValue(generator="user_id_seq", strategy = GenerationType.SEQUENCE)
      @SequenceGenerator(name="user_id_seq", sequenceName="user_id_seq")
      @Column(name = "id", nullable = false)
      private int id;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
      private Set&lt;UserLog&gt; userLogSet;
  
      // Other attributes
      // Getters/setters
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name = "user_log")
  public class UserLog
  {
      @Id
      @GeneratedValue(generator="user_log_id_seq", strategy = GenerationType.SEQUENCE)
      @SequenceGenerator(name="user_log_id_seq", sequenceName="user_log_id_seq")
      @Column(name = "id", nullable = false)
      private int id;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "user_id", nullable = false)
      private User user;
  
      // Other attributes
      // Getters/setters
  }
  </code></pre>
  
  <p>Whenever I try to delete a record from the "user" table, I get the following error from postgreSQL:</p>
  
  <blockquote>
    <p>ERROR: update or delete on table "user" violates foreign key
    constraint "fk228a019fd2495d20" on table "user_log"<br />Detail: Key
    (id)=(5) is still referenced from table "user_log".</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>Thank you!</p>
  
  <p><strong>Update:</strong> I tried coding a small example, it can be found <a href="https://github.com/MintTwist/Cascade-Test" rel="nofollow">here</a>. After launching the application with Tomcat the database schema is automatically created (check the connection details in /src/main/webapp/WEB-INF/jdbc.properties). When you visit <a href="http://localhost:8080" rel="nofollow">http://localhost:8080</a> some data is inserted in the database. If I try to delete any <code>teacher</code> who is referenced in the <code>student</code> table (using phpPgAdmin), I get an error similar to the one above.</p>