hibernate 的一个lazy问题,望解决,谢谢大家 lazy="true" 时多对多关系时容器会自动关闭session,当程序员懒惰时,系统就变得勤快起来,应该将lazy="false" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不是这样的,肯定要延迟加载的,不然系统的压力太大,而且N+1问题会大大降低查询效率。况且我已经指定fetch了并不会理会LAZY的值的。 即使不用hibernatetemplate也不能会出现上面的错误 恕我眼拙,我们看到这段代码里面,你的查询和session有什么关系Session session = this.getSession(); String hql = "select stu from Student stu join fetch stu.courses s join fetch s.teachers where stu.stuno = ? and stu.password=?"; String[] parameters = {name,password}; userList = getHibernateTemplate().find(hql,parameters); session.close(); 我没看到你拿到的session 在哪里用了!!呵呵!只看到关闭! 如果是spring +hibernate 建议google一下他们解决延迟加载的方案 给你一个例子session.beginTransaction();Query query=session.createQuery(str);list=query.list();Iterator iterator=list.iterator();while(iterator.hasNext()){ Obj_consignBill bill=(Obj_consignBill)iterator.next(); if(!Hibernate.isInitialized(bill.getBillID())){ session.refresh(bill); Hibernate.initialize(bill.getBillID()); }}session.getTransaction().commit();其中bill.getBillID就是set型的,以上这个方法是当lazy=true的时候用来获得所关联数据的方法 不好意思,session.close();那段代码是注释掉的。 代码重贴:public List checkStudent(String name, String password) { List userList = new ArrayList(); // TODO Auto-generated method stub Session session = this.getSession(); String hql = "select stu from Student stu join fetch stu.courses s join fetch s.teachers where stu.stuno = ? and stu.password=?"; /*String[] parameters = {name,password}; userList = getHibernateTemplate().find(hql,parameters);*/ Query query = session.createQuery(hql); query.setString(0,name); query.setString(1,password); userList = query.list(); /*Iterator it = ((Student)userList.get(0)).getCourses().iterator(); while (it.hasNext()) { Course course = (Course)it.next(); course.getTeachers().iterator(); }*/ session.close(); return userList; } 延迟加载 你需要在你使用userList打开session或者你用spring模板 session交给spring 2种方案1.使用OpenSessionInViewInterceptor或者OpenSessionInViewfilter.2.关系不表现在实体层,放在业务代码中。推荐第2种方法。 Tomcat正常启动后,总会抛出 Cannot assign requested address异常,求高手帮忙解决,在线等 javax.inject.Inject是哪个jar包里的 自定义异常类 jsp 页面文本框值累加 replace 替换问题,大家进来看下哈。。。。 J2ee什么是必须学习的 动态转静态的问题 很急 很急。。。。 基于J2EE平台开发一个B/S架构的网络教学平台,有兴趣接项目的进! 关于struts连jdbc出现的问题? 请高手介绍本好的struts书籍! spring 教材的选购? JSF+Spring+Hibernate框架中,系统用户需要安全的退出
String hql = "select stu from Student stu join fetch stu.courses s join fetch s.teachers where stu.stuno = ? and stu.password=?";
String[] parameters = {name,password};
userList = getHibernateTemplate().find(hql,parameters);
session.close(); 我没看到你拿到的session 在哪里用了!!呵呵!只看到关闭!
session.beginTransaction();
Query query=session.createQuery(str);
list=query.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Obj_consignBill bill=(Obj_consignBill)iterator.next();
if(!Hibernate.isInitialized(bill.getBillID())){
session.refresh(bill);
Hibernate.initialize(bill.getBillID());
}
}
session.getTransaction().commit();其中bill.getBillID就是set型的,以上这个方法是当lazy=true的时候用来获得所关联数据的方法
public List checkStudent(String name, String password) {
List userList = new ArrayList();
// TODO Auto-generated method stub
Session session = this.getSession();
String hql = "select stu from Student stu join fetch stu.courses s join fetch s.teachers where stu.stuno = ? and stu.password=?";
/*String[] parameters = {name,password};
userList = getHibernateTemplate().find(hql,parameters);*/
Query query = session.createQuery(hql);
query.setString(0,name);
query.setString(1,password);
userList = query.list();
/*Iterator it = ((Student)userList.get(0)).getCourses().iterator();
while (it.hasNext()) {
Course course = (Course)it.next();
course.getTeachers().iterator();
}*/
session.close();
return userList;
}
或者你用spring模板 session交给spring
1.使用OpenSessionInViewInterceptor或者OpenSessionInViewfilter.
2.关系不表现在实体层,放在业务代码中。
推荐第2种方法。