休眠一对多映射渴望获取不工作 [英] Hibernate one-to-many mapping eager fetch not working

查看:162
本文介绍了休眠一对多映射渴望获取不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学院和大学之间有一对多的关系。学生实体。



学院

  @Entity 
public班学院{

private int collegeId;
private String collegeName;
私人列表<学生>学生们;

@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(){
返回学生;
}

public void setStudents(List< Student> students){
this.students = students;
}

}

学生

  @Entity 
public class Student {

private int studentId;
private String studentName;
私立大学学院;

@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;
}

}

我是hibernate的新手,所以根据我的理解,如果我将fetchtype设置为 FetchType.EAGER ,那么每当我查询单个大学对象时,相关的学生对象都会自动获取。我使用了以下查询, / p>

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

college object is loaded properly,但when i比如 college.getStudents()作为回报,我会得到 null 。我错过了什么,或者这是正确的方式来获取eagarly。

解决方案

你的代码看起来不错,但你可以请尝试下面并让我们知道它是否有效。



College.java 中的代码行:

  @OneToMany(targetEntity = Student.class,mappedBy =college,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
公开名单<学生> getStudents(){
返回学生;
}

请尝试将其替换为:

  @OneToMany(targetEntity = Student.class,mappedBy =college,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name =college_id)//连接列在Student $ b $ public List< Student> getStudents(){
返回学生;
}

希望这可以帮到你。


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.

解决方案

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.

这篇关于休眠一对多映射渴望获取不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆