浅谈解决Hibernate懒加载的4种方式
作者:最美的词 时间:2022-07-04 00:46:39
本文总结了我在学习hibernate的过程中,解决hibernate懒加载问题的四种方式。
所谓懒加载(lazy)就是延时加载,延迟加载。
什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载。
至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,因为内存容量有限,为了减少并发量,减少系统资源的消耗,我们让数据在需要的时候才进行加载,这时我们就用到了懒加载。
例如,有一个对象是Employee,还有一个对象是Department。显然,对于Employee相对Department来说,是多对一的关系;而对于Department相对Employee来说,是一对多的关系。当我们查询Employee对象的时候,如果希望通过employee对象的属性department查询到所对应的Department,那么是会抛出异常的。这是因为懒加载的存在,在session关闭之后,hibernate又向数据库发出一次请求,结果就抛出异常了。
下面总结的是解决这个问题的四种方式:
1.显式初始化(在查询方法内部)
要查询某员工属于哪个部门的时候,需要对Department进行预先查询
使用语句
Hibernate.initialize(Department.class);
2.修改对象关系文件,将lazy改写lazy=false,即关闭懒加载
以上两种方法,确实可以解决问题,但是缺点是无论后面是否使用该对象,hibernate都会向数据库发出SQL语句请求数据,造成不必要的性能浪费。
3.使用过滤器(web项目)
①获取session的方式必须使用getCurrentSession
②特殊的关闭session方式
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();
}
}
4.在SSH框架中,使用spring提供的openSessionView
其原理和第三种方法中使用Filter类似,只不过这个filter是spring提供的。使用时只需要在web.xml文件配置如下:
<!-- 使用spring解决懒加载问题 -->
<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>
第3和第4中方法也能解决懒加载的问题,其中第4种方法也是目前使用较多的。但是这两种方法也是有缺点的,缺点就是延长了session关闭的时间,session的生命周期变长。没有使用该方法之前,session是在查询完数据之后,就被关闭了;而现在,session的关闭是在一次web请求的最后才关闭。
来源:http://blog.csdn.net/huhui_cs/article/details/8589977
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring MVC中接口参数解析的过程详解
![](https://img.aspxhome.com/file/2023/5/60525_0s.png)
Android上传文件到服务端并显示进度条
![](https://img.aspxhome.com/file/2023/3/72623_0s.jpg)
一文详解Java中Stream流的使用
![](https://img.aspxhome.com/file/2023/0/64800_0s.png)
英语单词state与status的区别
SpringBoot异步调用方法并接收返回值
![](https://img.aspxhome.com/file/2023/5/58055_0s.jpg)
spring异步service中处理线程数限制详解
springboot+vue实现登录功能的最新方法整理
![](https://img.aspxhome.com/file/2023/9/75679_0s.png)
浅谈Java中Spring Boot的优势
![](https://img.aspxhome.com/file/2023/5/83075_0s.png)
sin(x)如何求解的java代码实现方法
![](https://img.aspxhome.com/file/2023/3/64793_0s.jpg)
C++实现无重复字符的最长子串
java线程池ThreadPoolExecutor类使用小结
Java实体类不要使用基本类型的知识点总结
Springboot全局异常捕获及try catch区别解析
![](https://img.aspxhome.com/file/2023/7/59227_0s.png)
Spring深入刨析声明式事务
C#设计模式实现之迭代器模式
![](https://img.aspxhome.com/file/2023/8/82118_0s.png)
java实现波雷费密码算法示例代码
![](https://img.aspxhome.com/file/2023/3/62073_0s.png)
Java获取文件的类型和扩展名的实现方法
![](https://img.aspxhome.com/file/2023/3/82373_0s.png)
SpringBoot中@ConditionalOnBean实现原理解读
![](https://img.aspxhome.com/file/2023/2/82432_0s.png)
使用Maven Helper解决Maven插件冲突的方法
![](https://img.aspxhome.com/file/2023/1/58751_0s.jpg)
Java并发工具类LongAdder原理实例解析
![](https://img.aspxhome.com/file/2023/8/80328_0s.png)