使用Hibernate的Query cache

开发 后端
本文讲述如何使用Hibernate的Query cache。Hibernate中的QueryCache用来缓存查询语句,及查询结果集中对象的Identifier与Type。

老实说, 要做到在JDBC查询之前决定哪些数据需要从JDBC来还是CACHE来不是件容易事. 但是HIBERNATE还是很好地完成了这个任务. QueryCache用来缓存查询语句, 及查询结果集中对象的Identifier与Type. 当再次使用已缓存的Query时, 就可以通过对象的Identifier与Type在SECOND LEVEL CACHE中查找实际的对象.
 
使用hibernate中的QueryCache时需要在hibernate配置文件中设置如下属性:

  1. < property name="cache.provider_class"> 
  2.        org.hibernate.cache.HashtableCacheProvider  
  3. < /property> 
  4. < property name="hibernate.cache.use_query_cache">true< /property> 

建立ehcache的配置文件ehcache.xml放在classpath下

  1. < ehcache>   
  2.     < diskStore path="java.io.tmpdir"/>   
  3.     < defaultCache   
  4.         maxElementsInMemory="10000"   
  5.         eternal="false"   
  6.         timeToIdleSeconds="120"   
  7.         timeToLiveSeconds="120"   
  8.         overflowToDisk="true"   
  9.         />   
  10.    
  11.     < cache name="com.fhway.hibernate.bean.Employee"   
  12.         maxElementsInMemory="10"   
  13.         eternal="false"   
  14.         timeToIdleSeconds="100"   
  15.         timeToLiveSeconds="100"   
  16.         overflowToDisk="false"   
  17.         />   
  18.    
  19.     < cache name="com.fhway.hibernate.bean.Department"   
  20.         maxElementsInMemory="10"   
  21.         eternal="false"   
  22.         timeToIdleSeconds="100"   
  23.         timeToLiveSeconds="100"   
  24.         overflowToDisk="false"   
  25.         />   
  26.     
  27. < /ehcache> 

在配置文件里面要加入< cache>

  1. < class name="com.fhway.hibernate.bean.Employee " table=" Employee "> 
  2.         < cache usage="read-only"/> 

可以设定的策略包括read-only、read-write、nonstrict-read-write与transactional,并不是每一个第三方快取实现都支持所有的选项,每一个选项的使用时机与支持的产品,可以直接参考Hibernate官方参考快册的 20.2.The Second Level Cache;
 
在程序中需要为Query对象设置Cachable属性:

  1. Query query = sess.createQuery("from Employee as employee");  
  2. query.setCacheable(true);    
  3. List employees = (List) query.list();  
  4. Iterator iterator = employees.iterator();  
  5. while(iterator.hasNext()){  
  6.        System.out.println((Employee) iterator.next());  
  7. }  
  8.    
  9. Query query1 = sess.createQuery("from Employee as employee");  
  10. query1.setCacheable(true);    
  11. List employees1 = (List) query1.list();  
  12. Iterator iterator1 = employee1.iterator();  
  13. while(iterator1.hasNext()){  
  14.        System.out.println((Employee) iterator1.next());  
  15. }  
  16. Employee goncha = (Employee) sess.load(Employee.class, "001");  
  17. System.out.println(goncha);   

当你调用以上代码时你会发现这样的输出:

  1. Hibernate: select employee0_.ID as ID, employee0_.NAME as NAME0_, employee0_.DEPNO0 as DEPNO0_ from AFLYER.EMPLOYEE employee0_  
  2. com.fhway.hibernate.bean.Employee@e020c9  
  3. com.fhway.hibernate.bean.Employee@117f31e  
  4. com.fhway.hibernate.bean.Employee@bad8a8  
  5. com.fhway.hibernate.bean.Employee@104c575  
  6. com.fhway.hibernate.bean.Employee@e020c9  
  7. com.fhway.hibernate.bean.Employee@117f31e  
  8. com.fhway.hibernate.bean.Employee@bad8a8  
  9. com.fhway.hibernate.bean.Employee@104c575  
  10. com.fhway.hibernate.bean.Employee@e020c9 

很显然 该缓存的利用方式对Query和load()方式有效!
Query上有list()与iterator()方法,两者的差别在于list()方法在读取数据时,并不会利用到快取,而是直接再向数据库查询,而iterator()则将读取到的数据写到快取,并于读取时再次利用。(Blob 不能使用cache)

【编辑推荐】

  1. Hibernate中generator属性的意义
  2. hibernate Key Generator 主键生成方式
  3. Hibernate的主键生成机制
  4. Hibernate缓存概述
  5. Hibernate中hbm的generator属性
责任编辑:book05 来源: 和讯博客
相关推荐

2009-06-17 16:08:58

Hibernate C

2009-09-22 10:50:04

Hibernate c

2009-06-18 09:47:50

2009-09-24 18:11:56

Hibernate q

2010-07-05 09:07:42

2009-09-21 17:09:38

Hibernate C

2009-09-21 17:17:11

Hibernate二级

2009-06-18 12:59:39

Criteria Qu深入浅出Hiberna

2012-11-14 13:51:13

Mysqlquery cache

2010-05-19 16:39:11

MySQL查询

2009-06-26 10:19:00

Clob字段Hibernate

2009-06-26 10:32:00

QBC查询Hibernate

2009-06-01 11:51:37

hibernate缓存机制开发者

2009-09-28 13:43:28

使用Hibernate

2009-09-29 16:48:42

Hibernate J

2009-09-23 10:14:10

Hibernate使用

2009-09-25 13:55:05

Hibernate使用

2009-09-23 10:28:49

使用Hibernate

2009-06-30 16:52:30

DetchedCrit

2009-09-21 17:23:49

Hibernate使用
点赞
收藏

51CTO技术栈公众号