Hibernate中解决懒加载的方法

                                                               Hibernate中解决懒加载的方法

第一种:

1.在需要禁用懒加载的映射文件中显示的加入lazy = "false"

     这个方法大大的降低了程序的运行效率,如果访问量小还是可以的

第二种:

2.在web.xml中配置

        <filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

         这是一个一劳永逸解决懒加载问题的办法.使用spring的openSessionInViewFilter.
 openSessionInViewFilter,改过滤器在view渲染时始终开启session,一劳永逸解决hibernate的懒加载问题,
 该过滤器必须配置在struts2过滤器之前,如果访问增大性能会降低不推荐使用(性能问题)

第三种:
3.强行在service层面是初始化代理对象.
就是在获取对象之后,强行去加载对象中属性集合(推荐)

例如:

      public Department getDepartmentWithChildren(Integer id){
Department s = DepartmentDao.getEntity(id);
//强行初始化pages和questions集合
for(Student  stu : s.getStudents()){
stu.getClasses.size();
}
return s; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值