在controller中查询出了一个数据,数据关联了另外一个对象,在页面中用A.GETB().GETXXX()的方式的时候就会报错,因为是懒加载,默认的session已经关闭了。
解决问题的方式有以下几种
1.最常用的方式,如果是SSH框架的话,可以这么用。在配置文件中加入以下内容
<!-- 使用spring解决懒加载问题
但是这种方法也是有缺点的,缺点就是延长了session关闭的时间,session的生命周期变长。
没有使用该方法之前,session是在查询完数据之后,就被关闭了;而现在,session的关闭是在一次web请求的最后才关闭。
-->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.使用一个过滤器,如果是web项目的话
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
arg2.doFilter(request, response);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
if(tx != null){
tx.rollback();
}
}finally{
//特殊的关闭方式
HibernateUtil.closeCurrentSession();
}
}
3.将关联的对象改为立即加载
(但是会有问题,如果多个对象都立即加载,行程立即加载链条,会引起数据库风暴:比如A->B->C->D…..没准一个不小心,就会引出一个大查询:比如数据库日志,一下子几千万条数据),所以要小心这种方式
fetch = FetchType.LAZY-》fetch = FetchType.EAGER
4.在查询的时候,进行显示的查询。
这样也会有麻烦。太多代码要写
强行在service层面是初始化代理对象.
例如:
public Department getDepartmentWithChildren(Integer id){
Department s = DepartmentDao.getEntity(id);
//强行初始化pages和questions集合
for(Student stu : s.getStudents()){
stu.getClasses.size();
}
return s;
}
以上几种办法各有优缺点,看情况自己选择吧