用例子理解hibernate的一级缓存
2015-11-20 23:44
267 查看
缓存由ORM框架提供以便用户可以获得更快速的访问速度,同时框架本身也减少了数据库查询操作。
hibernate中的一级缓存是默认使用的,使用它你不需要操作任何事情。实际上,你不能强制关闭它。
如果我们我们知道一级缓存是和Session对象相关连的,那么理解一级缓存就会变得容易多了。我们知道Session对象由Session工厂创建,Session关闭的时候,对象丢失。同样的一级缓存只有在Session存活的时候才会有效,并且只在本Session中有效,在应用程序的其他Session中无效。
2、缓存的范围是Session对象,一旦Session关闭缓存的对象将会永久失效。
3、一级缓存是默认启用的,你不能让他失效。
4、当第一次查询一个实体的时候,会从数据库中获取并储存在和Session关联的一级缓存中。
5、如果我们在同一个Session中再一次查询相同的实体,它将会在缓存中加载SQL查询不会再次被执行。
6、加载的实体可以使用evict()方法被移除。如果使用了这个方法,再次加载相同的实体会发生数据库查询调用。
7、整个的Session中缓存的实体可以通过使用clear()方法移除,这会移除所有的缓存数据。
下面我们使用例子来证实上边提到的问题
我们可以看到第二次查询没有执行SQL语句而是直接加载了实体。
你可以看到,尽管实体已经存在于Session对象中了,但是当使用另一个Session对象sessionTemp的时候依旧会执行数据库查询语句。
使用新的Session,实体会被再一次的在数据库中被检索,尽管在其他的Session中实体也已经存在了。
evict()
clear()
这里的evict()方法用于清除缓存中特定的对象,clear()方法用于清除Session范围内所有的缓存的对象。
很明显,evict()方法移除缓存中的实体对象并且在一次的在数据库中获取对象。
翻译不是很准确,还请大家指正,大家有读不懂的可以参看原文,本人也在学习的过程中
原文地址:http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/
hibernate中的一级缓存是默认使用的,使用它你不需要操作任何事情。实际上,你不能强制关闭它。
如果我们我们知道一级缓存是和Session对象相关连的,那么理解一级缓存就会变得容易多了。我们知道Session对象由Session工厂创建,Session关闭的时候,对象丢失。同样的一级缓存只有在Session存活的时候才会有效,并且只在本Session中有效,在应用程序的其他Session中无效。
重要的事实:
1、一级缓存只在本Session中有效,在应用程序中的其他Session中无效。2、缓存的范围是Session对象,一旦Session关闭缓存的对象将会永久失效。
3、一级缓存是默认启用的,你不能让他失效。
4、当第一次查询一个实体的时候,会从数据库中获取并储存在和Session关联的一级缓存中。
5、如果我们在同一个Session中再一次查询相同的实体,它将会在缓存中加载SQL查询不会再次被执行。
6、加载的实体可以使用evict()方法被移除。如果使用了这个方法,再次加载相同的实体会发生数据库查询调用。
7、整个的Session中缓存的实体可以通过使用clear()方法移除,这会移除所有的缓存数据。
下面我们使用例子来证实上边提到的问题
一级缓存检索的例子
在这个例子中,我们将在数据库使用hibernate Session中检索DepartmentEntity 对象。我将会检索多次,并且观察SQL日志来查看他们的区别。//Open the hibernate session Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //fetch the department entity from database first time DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //fetch the department entity again department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); session.getTransaction().commit(); HibernateUtil.shutdown(); Output: Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource Human Resource
我们可以看到第二次查询没有执行SQL语句而是直接加载了实体。
一级缓存在新的Session中检索的例子。
//Open the hibernate session Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Session sessionTemp = HibernateUtil.getSessionFactory().openSession(); sessionTemp.beginTransaction(); try { //fetch the department entity from database first time DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //fetch the department entity again department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); department = (DepartmentEntity) sessionTemp.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); } finally { session.getTransaction().commit(); HibernateUtil.shutdown(); sessionTemp.getTransaction().commit(); HibernateUtil.shutdown(); } Output: Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource Human Resource Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource
你可以看到,尽管实体已经存在于Session对象中了,但是当使用另一个Session对象sessionTemp的时候依旧会执行数据库查询语句。
在一级缓存中移除缓存对象的例子
尽管我们不能在hibernate中将一级缓存置为无效,但是我们可以在需要的时候移除一些对象。下边是我们使用的两个方法使用新的Session,实体会被再一次的在数据库中被检索,尽管在其他的Session中实体也已经存在了。
evict()
clear()
这里的evict()方法用于清除缓存中特定的对象,clear()方法用于清除Session范围内所有的缓存的对象。
//Open the hibernate session Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); try { //fetch the department entity from database first time DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //fetch the department entity again department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); session.evict(department); //session.clear(); department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); } finally { session.getTransaction().commit(); HibernateUtil.shutdown(); } Output: Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource Human Resource Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource
很明显,evict()方法移除缓存中的实体对象并且在一次的在数据库中获取对象。
翻译不是很准确,还请大家指正,大家有读不懂的可以参看原文,本人也在学习的过程中
原文地址:http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/
相关文章推荐
- Populating Next Right Pointers in Each Node
- 1. Spring Boot 1.2.5正式发布
- 【bzoj2733】 [HNOI2012] 永无乡
- Qt查询MYSQL中文乱码问题
- tcp_tw_recycle和tcp_timestamps导致connect失败问题13695
- 群集和ipvs初探(一)
- 关于APP在安卓系统上网络异常的故障分析.
- Linux教程【一】
- 安卓命名规范
- Win10 UWP 开发系列:支持异步的SQLite
- lsof处理df和du大小不一致的问题
- 产品经理入门必读干货 | PMCAFF精选问答整理(不断更新)
- WIFI P2P (WIFI直连)源码解析
- HTML5获取地理位置
- c# 读取txt文件中特定内容后的文字
- HeartBeat+MySQL+DRBD
- 根据输入显示一个人的出生日期(我的第二个python程序)
- 《c语言从入门到精通》看书笔记——第16章 网络套接字编程(下)——套接字
- 安卓任务管理器的设计与实现
- 数据结构作业