Hibernate 未能延迟初始化角色集合:com.pojo.Student.phonenos,未关闭任何会话或会话
我正在学习使用注释进行hibernate映射。我已经完成了一个部分。即,我可以在保存父表时自动插入子类。 但是我在取主表时没有得到子表。还有一个错误Hibernate 未能延迟初始化角色集合:com.pojo.Student.phonenos,未关闭任何会话或会话,hibernate,annotations,lazy-loading,one-to-many,lazy-initialization,Hibernate,Annotations,Lazy Loading,One To Many,Lazy Initialization,我正在学习使用注释进行hibernate映射。我已经完成了一个部分。即,我可以在保存父表时自动插入子类。 但是我在取主表时没有得到子表。还有一个错误 failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed 我的代码添加到这里是为了让您复习。请检查一下。给我一个很好的建议。 java。
failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
我的代码添加到这里是为了让您复习。请检查一下。给我一个很好的建议。
java。(父类)
我的刀课
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;
}
这里我使用单向(外键)一对多映射(不是联合表,双向)
总结我的问题
1) 如何在获取父表时获取子表,反之亦然
2) 什么是渴望和懒惰
3) 单向、双向和联接表,在一对多映射的情况下,哪个映射的功率更大。1)
如果您真的希望每次检索这些类的任何实体时都执行此操作,请在@OneToMany
关联中指定<代码>@ManyToOne默认情况下是渴望的。请注意,如果仅在特定情况下需要获取这些实体,那么这可能在很大程度上是不够的。如果是这种情况,您必须像现在这样做,但请确保检索Student
对象的会话仍处于打开状态。看到您正在使用Spring,您是否尝试过使用@Transactional
注释DAO/服务,以便会话在方法执行期间保持活动状态?或者您是否尝试过使用Hibernate.execute()
,如下所示:
getHibernateTemplate().execute(new HibernateCallback(){
@SuppressWarnings("unchecked")
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Criteria c = s.createCriteria(Student.class);
List<Student> studentList = c.list();
for(Student st :studentList){
st.getPhoneNos();
}
}
});
getHibernateTemplate().execute(新的HibernateCallback()){
@抑制警告(“未选中”)
公共对象doInHibernate(会话s)引发HibernateeException,SQLException{
标准c=s.createCriteria(学生.班级);
List studentList=c.List();
学生名单(学生名单){
圣赫特菲诺斯();
}
}
});
2) 看看这个问题:
3) 这取决于你需要什么。如果只需要以一种方式导航关联,请仅以这种方式将其定义为单向关联。如果你两个都需要,两个都做。联接表更多地与数据库设计有关。如果您希望在
电话
表中有一个FK,其中引用了电话所属的学生
,或者希望有一个联接表,其中包含每个学生的电话
代码的图片不工作。。。getHibernateTemplate().execute(新的HibernateCallback()编译错误.Tnx.you for replay..需要解决方案。您是否尝试使用@Transactional
注释DAO或方法?您的applicationContext.xml中是否有transactionManager?
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
getHibernateTemplate().execute(new HibernateCallback(){
@SuppressWarnings("unchecked")
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Criteria c = s.createCriteria(Student.class);
List<Student> studentList = c.list();
for(Student st :studentList){
st.getPhoneNos();
}
}
});