未能延迟初始化角色集合:com.pojo.Student.phonenos,没有会话或会话已关闭
我正在学习使用注释的休眠映射。我已经完成了一节。即,当我保存父表时,我可以自动插入子类。 参见。
但是我在获取主表的时候并没有获取到子表。还收到错误
failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
我的代码已添加到此处以供您审核。请看一下。并给我很好的建议。 学生.java。 (父类)
@Entity
@Table(name="STUDENT")
public class Student {
private int studentid;
private String studentName;
private Set <Phone> phonenos;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="studenId")
public int getStudentid() {
return studentid;
}
public void setStudentid(int studentid) {
this.studentid = studentid;
}
@Column(name="studenName")
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="studenId")
public Set<Phone> getPhonenos() {
return phonenos;
}
public void setPhonenos(Set<Phone> phonenos) {
this.phonenos = phonenos;
}
Phone.java(子类)
@Entity
@Table(name = "PHONE")
public class Phone {
private int phoneid;
private String phoneNo;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "phoneId")
public int getPhoneid() {
return phoneid;
}
public void setPhoneid(int phoneid) {
this.phoneid = phoneid;
}
@Column(name = "phoneno")
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
我的dao类
public List<Student> getAllStudent() {
List<Student> studentList = null;
try {
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
studentList = (List<Student>)getHibernateTemplate().findByCriteria(criteria);
if(studentList != null && ! studentList.isEmpty()){
for(Student st :studentList){
System.out.println(" st name : "+st.getStudentName());
if(st.getPhonenos() != null && ! st.getPhonenos().isEmpty()){
for(Phone ph : st.getPhonenos()){
System.out.println(" ph no : "+ ph.getPhoneNo());
}
}else{
System.out.println(" phone number is null");
}
}
}else{
System.out.println(" student null");
}
} catch (DataAccessException e) {
e.printStackTrace();
}
return studentList;
}
输出是
failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
在这里我使用单向(外键)一对多映射(不是联合表,双向)。
总结我的问题
1)当我们获取父表时如何获取子表,反之亦然
2)什么是急切和惰性获取。
3)单向、双向、一对多映射情况下的联表哪个更强大。
I am learning hibernate mapping using annotation. I have completed one section. I.e. I can insert child class automatically when I save the parent table.
see_that.
But I did n't get the child table when I am fetching the master table. Also getting an error
failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
My code is added here for to review you. Please go through it. And give me the great advice.
Student.java. ( parent class)
@Entity
@Table(name="STUDENT")
public class Student {
private int studentid;
private String studentName;
private Set <Phone> phonenos;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="studenId")
public int getStudentid() {
return studentid;
}
public void setStudentid(int studentid) {
this.studentid = studentid;
}
@Column(name="studenName")
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="studenId")
public Set<Phone> getPhonenos() {
return phonenos;
}
public void setPhonenos(Set<Phone> phonenos) {
this.phonenos = phonenos;
}
Phone.java (child class)
@Entity
@Table(name = "PHONE")
public class Phone {
private int phoneid;
private String phoneNo;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "phoneId")
public int getPhoneid() {
return phoneid;
}
public void setPhoneid(int phoneid) {
this.phoneid = phoneid;
}
@Column(name = "phoneno")
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
my dao class
public List<Student> getAllStudent() {
List<Student> studentList = null;
try {
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
studentList = (List<Student>)getHibernateTemplate().findByCriteria(criteria);
if(studentList != null && ! studentList.isEmpty()){
for(Student st :studentList){
System.out.println(" st name : "+st.getStudentName());
if(st.getPhonenos() != null && ! st.getPhonenos().isEmpty()){
for(Phone ph : st.getPhonenos()){
System.out.println(" ph no : "+ ph.getPhoneNo());
}
}else{
System.out.println(" phone number is null");
}
}
}else{
System.out.println(" student null");
}
} catch (DataAccessException e) {
e.printStackTrace();
}
return studentList;
}
Out put is
failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
Here I am using unidirectional (foreign key) one-To-many mapping (not joint table, bidirectional).
Summerizing my question
1) how to get child table when we fetch the parent table, vice versa
2) what is eager and lazy fetch.
3) unidirection, bidirection, and join table in the case of One-To-Many mapping which one is more power full.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)
如果您确实希望每次检索这些类的任何实体时都执行此操作,请指定
@OneToMany
关联中的FetchMode.EAGER
。@ManyToOne
默认情况下是 eager。请注意,如果您只需要在特定情况下获取这些实体,这可能会效率很低。如果是这种情况,您必须像现在一样进行操作,但要确保检索Student
对象的会话仍处于打开状态。看到您正在使用 Spring,您是否尝试过使用@Transactional
注释您的 DAO/Service,以便会话在方法执行期间保持活动状态?或者您是否尝试过使用Hibernate.execute()
,如下所示:
2)看一下这个问题:Java持久化中FetchType LAZY和EAGER之间的区别?。
3)这取决于你的需要。如果您只需要以一种方式导航关联,则仅以这种方式定义它单向。如果两者都需要,就两者都做。 Join Table 更多地与数据库设计有关。如果您希望在
Phone
表中有一个 FK,并引用该电话所属的Student
,或者您希望在Phones 中有一个联接表
每个学生
。1)
If you want really to do this every time that any entity of these classes is retrieved, specify
FetchMode.EAGER
in the@OneToMany
association.@ManyToOne
are eager by default. Be aware that this might be largely unefficient if you need to fetch those entities only in particular circumstances. If that's the case, you have to do it like you're doing, but make sure that the session that retrieved theStudent
object is still open. Seeing that you're using Spring, have you tried annotating your DAO/Service with@Transactional
, so that the session keeps alive during method execution? Or have you tried usingHibernate.execute()
, like this:2) Take a look at this question: Difference between FetchType LAZY and EAGER in Java persistence?.
3) It depends on what you need. If you only need to navigate the association in one way, define it unidirectional in that way only. If you need both, do both. Join Table has more to do with database design. If you want to have a FK in the
Phone
table with the reference to theStudent
the phone belongs to, or you want to have a join table with thePhones
of everyStudent
.