1 Reply Latest reply on Jul 30, 2009 3:51 PM by Juan Martin Chiaradia

    ManyToMany :Constraint exception

    raja garlapati Newbie
      Hello All ,
         I am creating a simple many to many relation ship example , but i am getting this contraint exception:

      Here is my code:

      package org.domain.bannerStore.entity;

      import java.util.HashSet;
      import java.util.Set;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.Table;


      @Entity
      @Table(name = "student")
      public class Student implements java.io.Serializable {

          private static final long serialVersionUID = 1L;
         
          private Long id;
         
          private String name;

          private Set<Course> courses = new HashSet<Course>(0);


          @ManyToMany(cascade=CascadeType.ALL)
          @JoinTable(name = "student_course",
                      joinColumns = {@JoinColumn(name = "student_id", nullable=false, insertable=false)},
                      inverseJoinColumns={@JoinColumn(name="course_id", nullable=false, insertable=false)}  
          )
          public Set<Course> getCourses(){
            return courses;
          }
         
          public void setCourses(Set<Course> courses){
            this.courses = courses;
          }

          @Id
          @GeneratedValue(strategy = GenerationType.SEQUENCE)
          @Column(name = "id")
          public Long getId() {return id;}

          public void setId(long id) {this.id = id;}

          @Column(name = "name")
          public String getName() {
            return name;
          }
          public void setName(String s){
            name=s;
          }
        }
         
      //////////////////////////Course Class;
      package org.domain.bannerStore.entity;

      import java.util.HashSet;
      import java.util.Set;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.Table;


      @Entity
      @Table(name = "course")
      public class Course implements java.io.Serializable {

          private static final long serialVersionUID = 1L;
         
          private Long id;
         
          private String name;
         
          private Set<Student> students = new HashSet<Student>(0);
         

          @ManyToMany(cascade=CascadeType.ALL)   
          @JoinTable(name = "student_course",
                      joinColumns = {@JoinColumn(name = "course_id", nullable=false, insertable=false)},
                      inverseJoinColumns={@JoinColumn(name="student_id", nullable=false, insertable=false)}  
          )
          public Set<Student> getStudents(){
            return students;
          }
         
          public void setStudents(Set<Student> lefties){
            this.students = lefties;
          }

          @Id
          @GeneratedValue(strategy = GenerationType.SEQUENCE)
          @Column(name = "id")
          public Long getId() {return id;}

          public void setId(long id) {this.id = id;}

          @Column(name = "name")
          public String getName() {
            return name;
          }
          public void setName(String s){
            name=s;
          }

        }
         
      /////////////Test method
              @Override
              public String persist() {
                              Student student = new Student();
                              student.setName("Jason");
                             
                              Course course = new Course();
                              course.setName("J2EE");
                             
                              course.getStudents().add(student);
                             
                              return super.persist();
             }



      I Am doing it right and do i need to mention the join table class too, Any one implemented Many to Many relationship's ?
        • 1. Re: ManyToMany :Constraint exception
          Juan Martin Chiaradia Newbie

          Hi,
          try this:


          In Student class:



          @ManyToMany
          public Set<Course> getCourses(){
                return courses;
          }
          public void setCourses(Set<Course> courses){
                this.courses = courses;
          }



          in Course class:




          @ManyToMany(mappedBy="courses")
          public Set<Student> getStudents(){
                return students;
          }
          public void setStudents(Set<Student> lefties){
                this.students = lefties;
          }