例子:
model:
@Entity
//@BatchSize(size=5)
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Category {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试:
package com.hibernate.test;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.hibernate.model.Category;
import com.hibernate.model.Topic;
public class HQLTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass() {
// new SchemaExport(new
// AnnotationConfiguration().configure()).create(false, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(
false, true);
}
public static void main(String[] args) {
// new HQLTest().testSchemaExport();
HQLTest t = new HQLTest();
t.beforeClass();
t.testCache2();
}
@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(c);
session.save(t);
}
session.getTransaction().commit();
session.close();
}
//一级缓存
//只会发出一条查询语句
@Test
public void testCache1() {
Session session = sf.openSession();
session.beginTransaction();
Category c = (Category)session.load(Category.class, 1);
System.out.println(c.getName());
Category c2 = (Category)session.load(Category.class, 1);
System.out.println(c2.getName());
session.getTransaction().commit();
session.close();
}
/**
* 二级缓存
* 默认情况会发两条查询语句
*
* 设置二级缓存,加入包ehcache-1.2.3.jar,在发行包中(还有另外几种二级缓存的实现厂家)
*
* 加入配置文件ehcache.xml,加入commons-logging-1.1.1.jar包
*
* 更改hibernate.hbm.xml文件
* <property name="cache.use_second_level_cache">true</property>
*<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
*<property name="cache.use_query_cache">true</property>
*
*在Category类中加入注解, @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
*
*可以了,现在利用了二级缓存,只发出一条查询语句
*/
@Test
public void testCache2() {
Session session = sf.openSession();
session.beginTransaction();
Category c = (Category)session.load(Category.class, 1);
System.out.println(c.getName());
session.getTransaction().commit();
session.close();
Session session2 = sf.openSession();
session2.beginTransaction();
Category c2 = (Category)session2.load(Category.class, 1);
System.out.println(c2.getName());
session2.getTransaction().commit();
session2.close();
}
/**
* 查询缓存,查询条件一样
* 设置.setCacheable(true)
*/
@Test
public void testQueryCache3() {
Session session = sf.openSession();
session.beginTransaction();
List<Category> categories = (List<Category>)session.createQuery("from Category")
.setCacheable(true).list();
session.getTransaction().commit();
session.close();
Session session2 = sf.openSession();
session2.beginTransaction();
List<Category> categories2 = (List<Category>)session2.createQuery("from Category")
.setCacheable(true).list();
session2.getTransaction().commit();
session2.close();
}
}
缓存策略提供商(Cache Providers)
Cache | Provider class | Type | Cluster Safe | Query Cache Supported |
---|---|---|---|---|
Hashtable (not intended for production use) | org.hibernate.cache.HashtableCacheProvider | memory | yes | |
EHCache | org.hibernate.cache.EhCacheProvider | memory, disk | yes | |
OSCache | org.hibernate.cache.OSCacheProvider | memory, disk | yes | |
SwarmCache | org.hibernate.cache.SwarmCacheProvider | clustered (ip multicast) | yes (clustered invalidation) | |
JBoss Cache 1.x | org.hibernate.cache.TreeCacheProvider | clustered (ip multicast), transactional | yes (replication) | yes (clock sync req.) |
JBoss Cache 2 | org.hibernate.cache.jbc2.JBossCacheRegionFactory | clustered (ip multicast), transactional | yes (replication or invalidation) | yes (clock sync req.) |