您的位置:首页 > 其它

用例子理解hibernate的一级缓存

2015-11-20 23:44 267 查看
缓存由ORM框架提供以便用户可以获得更快速的访问速度,同时框架本身也减少了数据库查询操作。

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: