2

There is a one to 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 am a newbie to hibernate, so based on my understanding if i set the fetchtype as FetchType.EAGER then whenever i query for a single college object related student objects are fetched automatically.I have used following query,

College college = (College) session.get(College.class, id);

college object is loaded properly but when i say college.getStudents() in return i'll get null. Am i missing something or is this the proper way to fetch eagarly.

Amit
  • 465
  • 2
  • 6
  • 20
  • 1
    I have to ask it: Are you 100% sure that theres data. And please show us your service code. – mh-dev May 12 '15 at 05:48
  • That's impossible. Hibernate will never give you a null collection. If it was not eagerly loaded, it would load it lazily, and return a non-null list (empty if the college doesn't have any student). Show us a complete test reproducing the problem, turn SQL logging on, and tell us what *really* happens. The only way for this to happen is if you have just, yourself, in the same session, created the college and left the collection null. – JB Nizet May 12 '15 at 06:05
  • Please check the configured AccessType. The access attribute lets you control how Hibernate will access the property at runtime. By default, Hibernate will call the property get/set pair. If you specify access="field", Hibernate will bypass the get/set pair and access the field directly, using reflection. You may specify your own strategy for property access by naming a class that implements the interface org.hibernate.property.PropertyAccessor. – Suken Shah May 12 '15 at 06:09
  • If the collection would contain data and be loaded lazily, then it would throw a `LazyInitializationException` from hibernate (if no session is open). So make sure there is actually data mapped to that field. – DrunkenPope May 12 '15 at 06:25
  • sure your code is fine, please provide your service class code i think you missing the setCollege i.e student.setCollege(CollegeClassObject); – Zealous System May 12 '15 at 06:53
  • mh-dev / JB Nizet / Suken Shah - all you guys are correct i used to fetch records from db before actually committing the persist data. – Amit May 13 '15 at 03:21

1 Answers1

-1

Your code looks ok, but can you please try below and let us know is it 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 to replace this with :

@OneToMany(targetEntity=Student.class, mappedBy="college", cascade=CascadeType.ALL, fetch=FetchType.EAGER ) 
@JoinColumn(name="college_id") // join column is in table for Student
public List<Student> getStudents() {
    return students;
}

Hope this helps you.

vishal rana
  • 199
  • 1
  • 8