EJB3缓存 JPA环境下使用Hibernate二级缓存

3 篇文章 0 订阅
3 篇文章 0 订阅
hibernate二级缓存本质上分为两类: 
1.对象 缓存 
2.查询缓存 

在JPA环境下,例如Jboss,底层还是通过Hibernate来实现JPA的Query。 

下边简单说一下配置的步骤: 

1.配置entity 
在实体上方加入@Cache 
Java代码
  收藏代码
  1. import java.io.Serializable;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.Id;  
  6. import javax.persistence.Table;  
  7.   
  8. import org.hibernate.annotations.Cache;  
  9. import org.hibernate.annotations.CacheConcurrencyStrategy;  
  10.   
  11. @Entity  
  12. @Table  
  13. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  
  14. public class User implements Serializable {  
  15.     private static final long serialVersionUID = -5121812640999313420L;  
  16.   
  17.     private Integer id;  
  18.       
  19.     private String name;  
  20.   
  21.     @Id  
  22.     @GeneratedValue  
  23.     public Integer getId() {  
  24.         return id;  
  25.     }  
  26.   
  27.     public void setId(Integer id) {  
  28.         this.id = id;  
  29.     }  
  30.   
  31.     public String getName() {  
  32.         return name;  
  33.     }  
  34.   
  35.     public void setName(String name) {  
  36.         this.name = name;  
  37.     }  
  38.       
  39. }  

CacheConcurrencyStrategy有几种,大家自己查下相关资料,按需要配置就可以了,我这里不需要事务支持. 
需要注意的是,@Cache这个注解在很多jar包里都有,注意我上边写的import. 

2.配置EJB/META-INF/persistence.xml 
Xml代码
  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!-- Persistence deployment descriptor for dev profile -->  
  3. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
  6.     version="1.0">  
  7.   
  8.     <persistence-unit name="cachedb">  
  9.         <jta-data-source>java:/cachedb</jta-data-source>  
  10.         <properties>  
  11.             <property name="hibernate.hbm2ddl.auto" value="update" />  
  12.             <property name="hibernate.show_sql" value="true" />  
  13.             <property name="hibernate.format_sql" value="true" />  
  14.             <property name="hibernate.cache.use_second_level_cache" value="true" />  
  15.             <property name="hibernate.cache.use_structured_entries" value ="true" />  
  16.             <property name="hibernate.cache.use_query_cache" value="true" />  
  17.             <property name="hibernate.cache.provider_class" value="com.googlecode.hibernate.memcached.MemcachedCacheProvider" />  
  18.             <property name="hibernate.memcached.servers" value="localhost:11211" />  
  19.             <property name="hibernate.memcached.cacheTimeSeconds" value="300" />  
  20.         </properties>  
  21.     </persistence-unit>  
  22.   
  23. </persistence>  

这里我使用了memcached,还有Ehcache、OSchache、或者TreeCache等,主要需配置: 
hibernate.cache.use_second_level_cache = true 
hibernate.cache.use_query_cache = true 
与相应的hibernate.cache.provider_class 

3.在程序中使用查询缓存 
    首先大家需要明确JPA对缓存的规范还没有形成,但JPA实现的厂家都会用hibernate来做JPA的实现,所以通常的方法是将JPA的Query转换成Hibernate的Query,大家用过Hibernate都知道,Hibernate里的Query有个setCacheable(true/false)的方法,这里是设置查询是否进入二级缓存的. 
    这里需要强调一下,默认的如果不在程序中显示的执行查询缓存声明操作,hibernate是不会对查询的list进行缓存的,默认的在开启hibernate二级缓存时,hibernate只缓存,根据主键id查找的对象,jpa下是find(id, clazz)方法. 

下边是转换的代码:为了区分JPA的Query与Hibernate的Query,我写上了全名 
Java代码
  收藏代码
  1. public List<User> listUser() {  
  2. javax.persistence.Query query = em.createQuery("from User u where u.id>:id", User.class);  
  3. query.setParameter("id"5);  
  4. org.hibernate.ejb.QueryImpl hs = null;  
  5. org.hibernate.Query hbQuery = null;  
  6. List<User> list = null;  
  7. if(query instanceof org.hibernate.ejb.QueryImpl) {  
  8.     hs = (org.hibernate.ejb.QueryImpl)query;  
  9.     hbQuery = hs.getHibernateQuery();  
  10.     hbQuery.setCacheable(true);//设置使用二级缓存  
  11.     list = hbQuery.list();  
  12. else {  
  13.     list = query.getResultList();  
  14. }  
  15. return list;  
  16. }  


这里再说一下使用经验,这样的转换不光是对createQuery方法,还能对createNamedQuery,甚至是createNativeQuery,都可以缓存查询结果. 

只是一点使用经验,跟大家分享一下:) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值