Chaitanya
Chaitanya

Reputation: 2444

Hibernate many to many mapping

I have an existing database modeled the following way:

Student - SchoolId(PK), StudentId(PK), StudentName

Teacher - SchoolId(PK), TeacherId(PK), TeacherName

Student_Teacher - SchoolId(PK), StudentId(PK), TeacherId(PK)

Foreign key references exist from Student_Teacher to respective entities.

Now I am creating hibernate entities for this existing database. And I am running into weird issues creating Many-to-Many mapping from Student to Teacher.

@Entity
@Table(name = "Student")
public class Student {
    @EmbeddableId
    private StudentPK itemId;

    @Column(name="StudentName")
    private String studentName;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="Student_Teacher", joinColumns={@JoinColumn(name="SchoolId", referencedColumnName="SchoolId"),@JoinColumn(name="StudentId", referencedColumnName="StudentId")}, inverseJoinColumns={@JoinColumn(name="SchoolId", referencedColumnName="SchoolId"),@JoinColumn(name="TeacherId", referencedColumnName="TeacherId")})
    private List<Teacher> attachments=new ArrayList<Teacher>();
}

The above code compains about some duplicate SchoolId reference.

Any ideas?

Upvotes: 0

Views: 184

Answers (2)

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

As I see that there is an issue in your mapping of entities, It should be as follows

school - school_id(PK), school_name

student - student_id(PK) , student_name, fk_school_id(FK),

teacher - teacher_id(PK), teacher_name , fk_school_id(FK)

*student_teacher* - student_teacher_id(PK), fk_student_id(FK), fk_teacher_id(FK)

and Entity clasess as follows

School Entity

@Entity
@Table(name = "school")
public class School {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column (name = "school_id")
   private int Id;

    @Column(name="school_name")
    private String schoolName;  

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "school")
   private Set<Student> students = new HashSet<Student>  

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "school")
   private Set<Teacher> teachers = new HashSet<Teacher> 
}

Student Entity

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "student_id")
    private int Id;

    @Column(name="student_name")
    private String studentName;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "school_id", nullable = false)
   private School school;       

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "student_teacher", joinColumns = {@JoinColumn(name = "fk_student_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_teacher_id") })
    private List<Teacher> teachers = new ArrayList<Teacher>();

}

Teacher Entity

@Entity
@Table(name = "teacher")
public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "teacher_id")
    private int Id;

    @Column(name="teacher_name")
    private String name;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "school_id", nullable = false)
   private School school;   

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "student_teacher", joinColumns = {@JoinColumn(name = "fk_teacher_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_student_id") })
    private List<Student> students =new ArrayList<Student>();
}

hope this will solve this problem..

as you have declare 'SchoolId' as PK in Student_Teacher table it will not allow you to add duplicate entry for SchoolId field for Student_Teacher table and this is not the case. thus the above relationship will gives duplicate SchoolId reference. when you are going to add two different students from same school into Student_Teacher table..

Upvotes: 1

Filip
Filip

Reputation: 877

Did you define the various PKs per entity as compound keys, as i note that you use multiple PKs per entity. Is there any constraint why you can't use a sole PK per entity and just use a relation table to bind the 2 entities?

Upvotes: 0

Related Questions