对于懒加载问题简单的解决方式

一种看起来low的方式:在方法上面加上boolean类型参数用来判断是否进行初始化

    static Employee query(int id,boolean includeDepart){
        Session s=null;
        try{
            s=HibernateUtil.getSession();
            Employee emp=(Employee) s.get(Employee.class, id);
            if(includeDepart)
                Hibernate.initialize(emp.getDepart());
            return emp;
        }finally{
            if(s!=null) s.close();
        }
    }

第二种方式:使用onpenSessionInView解决。在过滤器中操作事务。

原文地址:https://www.cnblogs.com/aigeileshei/p/5803502.html