java - Hibernate one-to-many mapping eager fetch not working -
there 1 many relationship between college & student entities.
college
@entity public class college { private int collegeid; private string collegename; private list<student> students; @id @generatedvalue public int getcollegeid() { return collegeid; } public void setcollegeid(int collegeid) { this.collegeid = collegeid; } public string getcollegename() { return collegename; } public void setcollegename(string collegename) { this.collegename = collegename; } @onetomany(targetentity=student.class, mappedby="college", cascade=cascadetype.all, fetch=fetchtype.eager ) public list<student> getstudents() { return students; } public void setstudents(list<student> students) { this.students = students; } }
student
@entity public class student { private int studentid; private string studentname; private college college; @id @generatedvalue public int getstudentid() { return studentid; } public void setstudentid(int studentid) { this.studentid = studentid; } public string getstudentname() { return studentname; } public void setstudentname(string studentname) { this.studentname = studentname; } @manytoone @joincolumn(name="college_id") public college getcollege() { return college; } public void setcollege(college college) { this.college = college; } }
i newbie hibernate, based on understanding if set fetchtype fetchtype.eager
whenever query single college object related student objects fetched automatically.i have used following query,
college college = (college) session.get(college.class, id);
college
object loaded when college.getstudents()
in return i'll null
. missing or proper way fetch eagarly.
your code looks ok, can please try below , let know works or not.
your line of code in college.java :
@onetomany(targetentity=student.class, mappedby="college", cascade=cascadetype.all, fetch=fetchtype.eager ) public list<student> getstudents() { return students; }
please try replace :
@onetomany(targetentity=student.class, mappedby="college", cascade=cascadetype.all, fetch=fetchtype.eager ) @joincolumn(name="college_id") // join column in table student public list<student> getstudents() { return students; }
hope helps you.
Comments
Post a Comment