title: Hibernate many-to-many cascading delete
body: <p>I have 3 tables in my database: <code>Students</code>, <code>Courses</code> and <code>Students_Courses</code></p>
  
  <p>Students can have multiple courses and courses can have multiple students.  There is a many-to-many relationship between <code>Students</code> and <code>Courses</code>.</p>
  
  <p>I have 3 cases for my project and courses added to my <code>Courses</code> table.</p>
  
  <ul>
  <li>(a) When I add a user, it gets saved fine, </li>
  <li>(b) When I add courses for the student, it creates new rows in <code>User_Courses</code> - again, expected behaviour.</li>
  <li>(c) When I am trying to delete the student, it is deleting the appropriate records in <code>Students</code> and <code>Students_Courses</code>, but it is also deleting <code>Courses</code> records which is not required.  Even if I don't have any user in a course, I want the course to be there. </li>
  </ul>
  
  <p>Below is my code for tables and annotate classes.</p>
  
  <pre><code>    CREATE TABLE `Students` (
      `StudentID` INT(11) NOT NULL AUTO_INCREMENT,
      `StudentName` VARCHAR(50) NOT NULL 
      PRIMARY KEY (`StudentID`)
  )
  
  CREATE TABLE `Courses` (
      `CourseID` INT(11) NOT NULL AUTO_INCREMENT,
      `CourseName` VARCHAR(50) NOT NULL 
      PRIMARY KEY (`CourseID`)
  )
  
  CREATE TABLE `Student_Courses` (
      `StudentId` INT(10) NOT NULL DEFAULT '0',
      `CourseID` INT(10) NOT NULL DEFAULT '0',
      PRIMARY KEY (`StudentId`, `CourseID`),
      INDEX `FK__courses` (`CourseID`),
      INDEX `StudentId` (`StudentId`),
      CONSTRAINT `FK__courses` FOREIGN KEY (`CourseID`) REFERENCES `courses` (`CourseID`) ON DELETE NO ACTION,
      CONSTRAINT `FK_students` FOREIGN KEY (`StudentId`) REFERENCES `students` (`StudentId`)
  )
  </code></pre>
  
  <p>This is the Java code generated by Hibernate:</p>
  
  <pre><code>@Entity
  @Table(name = "Students")
  public class Students implements java.io.Serializable {
  
      private Integer StudentID;
       private String Students;
      private Set&lt;Courses&gt; Courseses = new HashSet&lt;Courses&gt;(0);
  
      public Students() {
      }
  
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "StudentID", unique = true, nullable = false)
      public Integer getStudentID() {
          return this.StudentID;
      }
  
      public void setStudentID(Integer StudentID) {
          this.StudentID = StudentID;
      }
  
      @Column(name = "Students", nullable = false, length = 50)
      public String getCampaign() {
          return this.Students;
      }
  
      public void setCampaign(String Students) {
          this.Students = Students;
      }
  
  
   @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
      @JoinTable(name = "Student_Courses", joinColumns = {
          @JoinColumn(name = "StudentId", nullable = false, updatable = false)}, inverseJoinColumns = {
          @JoinColumn(name = "CourseID", nullable = false, updatable = false)})
      public Set&lt;Courses&gt; getCourseses() {
          return this.Courseses;
      }
  
       public void setCourseses(Set&lt;Courses&gt; Courseses) {
          this.Courseses = Courseses;
      }
  
      }
  
  
      @Entity
  @Table(name = "Courses")
  public class Courses implements java.io.Serializable {
  
    private Integer CourseID;
      private String CourseName;
       private Set&lt;Students&gt; Studentses = new HashSet&lt;Students&gt;(0);
  
      public Courses() {
      }
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "CourseID", unique = true, nullable = false)
      public Integer getCourseID() {
          return this.CourseID;
      }
  
      public void setCourseID(Integer CourseID) {
          this.CourseID = CourseID;
      }
  
       @Column(name = "CourseName", nullable = false, length = 100)
      public String getCourseName() {
          return this.CourseName;
      }
  
      public void setCourseName(String CourseName) {
          this.CourseName = CourseName;
      }
  
      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "Courseses")
      public Set&lt;Students&gt; getStudentses() {
          return this.Studentses;
      }
  
      public void setStudentses(Set&lt;Students&gt; Studentses) {
          this.Studentses = Studentses;
      }
  
      }
  </code></pre>
  
  <p>How can I achieve what I have described?  I could not find any reasonable documentation on the web.</p>