Hibernate-------一级缓存/二级缓存/查询缓存

例子:

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)

CacheProvider classTypeCluster SafeQuery Cache Supported
Hashtable (not intended for production use)org.hibernate.cache.HashtableCacheProvidermemory yes
EHCacheorg.hibernate.cache.EhCacheProvidermemory, disk yes
OSCacheorg.hibernate.cache.OSCacheProvidermemory, disk yes
SwarmCacheorg.hibernate.cache.SwarmCacheProviderclustered (ip multicast)yes (clustered invalidation) 
JBoss Cache 1.xorg.hibernate.cache.TreeCacheProviderclustered (ip multicast), transactionalyes (replication)yes (clock sync req.)
JBoss Cache 2org.hibernate.cache.jbc2.JBossCacheRegionFactoryclustered (ip multicast), transactionalyes (replication or invalidation)yes (clock sync req.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值