http://tuhaitao.iteye.com/blog/568653
hibernate二级缓存本质上分为两类:
1.对象缓存2.查询缓存
在JPA环境下,例如Jboss,底层还是通过Hibernate来实现JPA的Query。
下边简单说一下配置的步骤:
1.配置entity
在实体上方加入@Cache
- import java.io.Serializable;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- @Entity
- @Table
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- public class User implements Serializable {
- private static final long serialVersionUID = -5121812640999313420L;
- private Integer id;
- private String name;
- @Id
- @GeneratedValue
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
CacheConcurrencyStrategy有几种,大家自己查下相关资料,按需要配置就可以了,我这里不需要事务支持.
需要注意的是,@Cache这个注解在很多jar包里都有,注意我上边写的import.
2.配置EJB/META-INF/persistence.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Persistence deployment descriptor for dev profile -->
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- version="1.0">
- <persistence-unit name="cachedb">
- <jta-data-source>java:/cachedb</jta-data-source>
- <properties>
- <property name="hibernate.hbm2ddl.auto" value="update" />
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.format_sql" value="true" />
- <property name="hibernate.cache.use_second_level_cache" value="true" />
- <property name="hibernate.cache.use_structured_entries" value ="true" />
- <property name="hibernate.cache.use_query_cache" value="true" />
- <property name="hibernate.cache.provider_class" value="com.googlecode.hibernate.memcached.MemcachedCacheProvider" />
- <property name="hibernate.memcached.servers" value="localhost:11211" />
- <property name="hibernate.memcached.cacheTimeSeconds" value="300" />
- </properties>
- </persistence-unit>
- </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,我写上了全名
- public List<User> listUser() {
- javax.persistence.Query query = em.createQuery("from User u where u.id>:id", User.class);
- query.setParameter("id", 5);
- org.hibernate.ejb.QueryImpl hs = null;
- org.hibernate.Query hbQuery = null;
- List<User> list = null;
- if(query instanceof org.hibernate.ejb.QueryImpl) {
- hs = (org.hibernate.ejb.QueryImpl)query;
- hbQuery = hs.getHibernateQuery();
- hbQuery.setCacheable(true);//设置使用二级缓存
- list = hbQuery.list();
- } else {
- list = query.getResultList();
- }
- return list;
- }
这里再说一下使用经验,这样的转换不光是对createQuery方法,还能对createNamedQuery,甚至是createNativeQuery,都可以缓存查询结果.
只是一点使用经验,跟大家分享一下:)