title: Hibernate : Not allowing cascaded delete because of Foreign key violations
body: <p>I am working on a Spring-MVC application in which I am trying to delete an entity, which has one-to-many relationships with 3 other entities. I have marked the entities to be deleted on Cascade, but it is not working. I am getting an error of foreign key violation..</p>
  
  <p>Notes Entity :</p>
  
  <pre><code>@Entity
  @Table(name="groupnotes")
  public class GroupNotes {
  
      @OneToMany(mappedBy = "mnotedata",fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
      private Set&lt;GroupAttachments&gt; mattachments = new HashSet&lt;&gt;();
  
      public Set&lt;GroupAttachments&gt; getMattachments() {
          return this.mattachments;
      }
  
      public void setMattachments(Set&lt;GroupAttachments&gt; mattachments) {
  
          this.mattachments = mattachments;
      }
  
      @OneToMany(mappedBy = "mhistory",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      private Set&lt;GroupNoteHistory&gt; groupNoteHistorySet = new HashSet&lt;&gt;();
  
      public Set&lt;GroupNoteHistory&gt; getGroupNoteHistorySet(){
          return this.groupNoteHistorySet;
      }
  
      public void setGroupNoteHistorySet(Set&lt;GroupNoteHistory&gt; groupNoteHistorySet){
          this.groupNoteHistorySet = groupNoteHistorySet;
      }
  
      @OneToMany(mappedBy = "unreadNotes",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      private Set&lt;UnreadNotes&gt; unreadNotesSet = new HashSet&lt;&gt;();
  
      public Set&lt;UnreadNotes&gt; getUnreadNotesSet(){
          return this.unreadNotesSet;
      }
  
      public void setUnreadNotesSet(Set&lt;UnreadNotes&gt; unreadNotesSet){
          this.unreadNotesSet = unreadNotesSet;
      }
  }
  </code></pre>
  
  <p>Entity GroupAttachments :</p>
  
  <pre><code>@Entity
  @Table(name = "groupattachments")
  public class GroupAttachments {
  @ManyToOne
      @JoinColumn(name = "mnoteid")
      @OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
      private GroupNotes mnotedata;
  
      public void setMnotedata(GroupNotes mnotedata){this.mnotedata=mnotedata;}
  
      public GroupNotes getMnotedata(){return mnotedata;}
  }
  </code></pre>
  
  <p>But when I execute the code below :</p>
  
  <pre><code> @Override
      public boolean deleteAllNotesForSection(int msectionid) {
          session = this.sessionFactory.getCurrentSession();
          org.hibernate.Query query = session.createQuery("from GroupNotes as " +
                  "n where n.ownednotes.msectionid=:msectionid");
          query.setParameter("msectionid",msectionid);
          List&lt;GroupNotes&gt; groupNotesList = query.list();
          for(GroupNotes groupNotes : groupNotesList){
              groupNotes.getGroupNoteHistorySet().clear();
              groupNotes.getMattachments().clear();
              groupNotes.getUnreadNotesSet().clear();
              session.flush();
          }
  
          org.hibernate.Query query1 = session.createQuery("delete from GroupNotes as " +
                  "n where n.ownednotes.msectionid=:msectionid");
          query1.setParameter("msectionid",msectionid);
          query1.executeUpdate();
          session.flush();
          return true;
      }
  </code></pre>
  
  <p>I get the following error when I execute the above code :</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
  org.postgresql.util.PSQLException: ERROR: update or delete on table "groupnotes" violates foreign key constraint "groupnotes_groupnotehistory_fk" on table "groupnotehistory"
    Detail: Key (mnoteid)=(5455) is still referenced from table "groupnotehistory".
  </code></pre>
  
  <p>Isn't the first query which clears all the set, should clear from the database the history and all. And ideally there shouldn't be a problem, as it is cascade delete. I also tried retrieving the List using query1.list and deleting in a for-loop, that also didn't work. I don't understand how to remedy this. Thanks a lot.</p>