java - Hibernate retrieve wrong list of objects in one-to-many relation -


i'm new hibernate. have tables department , teacher. 1 department can have many teachers, 1 teacher can attached 1 department. have following mapping:

@entity @table(name = "department") public class department {     @id     @generatedvalue(strategy = generationtype.identity)     @column(name = "id")     private integer id;      @column(name = "name")     private string name;      @column(name = "description")     private string description;      @onetomany (cascade=cascadetype.all, fetch = fetchtype.eager)     @joincolumn(name = "department_id")     private list<teacher> teachers = new arraylist<teacher>(); } 

and

@entity @table(name = "teacher") public class teacher {     @id     @generatedvalue(strategy = generationtype.identity)     @column(name = "id")     private integer id;      @column(name = "fname")     private string fname;      @column(name = "lname")     private string sname;      @column(name = "degree")     private string degree;      @manytoone     private department department; } 

also there getters , setters, omitted simplicity. i'm using mysql database. when trying insert new department new teachers, works fine. but, want able full list of departments teachers, assigned each department. in database have 5 departments , 13 teachers. when executing following code:

    departmentdaoimpl ddi = new departmentdaoimpl();     list<department> departments = ddi.getalldepartment();      (department k : departments){         printdepartmentdata(k);         list<teacher> teachers = k.getteachers();         (teacher t : teachers){             printteachername(t);         }     } 

and getalldepartments implementation:

@override public arraylist getalldepartment() throws sqlexception {     session session = null;     arraylist<department> result = null;     try{         session = hibernateutil.getsessionfactory().opensession();         result = (arraylist)session.createcriteria(department.class).list();     }catch(exception ex){         ex.printstacktrace();     }finally{         if(session != null && session.isopen()){             session.close();         }     }     return result; } 

sql create tables:

create table teacher ( id integer primary key auto_increment, fname varchar(30), lname varchar(30), degree varchar(100), department_id integer ); create table department( id integer primary key auto_increment, name varchar(100), description varchar(5000)  ); 

i 13 instances of department. , 5 instances of first department (i have 5 teachers assigned it) each of them have same 5 teacher assigned, 2 instances of second department (i have 2 teachers assigned it) each of them have 2 same teachers assigned , on. can figure out, how fix it? need 5 instances of department, with
corresponding number of teachers.

since configure 1 many relationship fetch = fetchtype.eager, hibernate performs join on 2 tables. result see caused join.

make sure have correct equals() , hashcode() methods in both department , teacher. if not solve problem, try use set instead of list.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -