java - Hibernate many to many mapping -


i have existing database modeled 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 student_teacher respective entities.

now creating hibernate entities existing database. , running weird issues creating many-to-many mapping student 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 duplicate schoolid reference.

any ideas?

as see there issue in mapping of entities, should 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 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 solve problem..

as have declare 'schoolid' pk in student_teacher table not allow add duplicate entry schoolid field student_teacher table , not case. above relationship gives duplicate schoolid reference. when going add 2 different students same school student_teacher table..


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -