首页 > 代码库 > Hibernate二级缓存

Hibernate二级缓存

Hibernate包括两个级别的缓存:
Session(默认启动)一级缓存
SessionFactory(默认关闭)二级缓存
二级缓存速度快
一旦在应用中开启了SessionFactory二级缓存,那么Session默认使用二级缓存
开启二级缓存要在hibernate.cfg.xml中添加:
<!-- 开启二级缓存缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 设置二级缓存的实现类-->

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>



将缓存实现所需的配置文件添加到类加载路径中,对于EHCashe缓存,它需要一个ehcacha.xml配置文件:

<?xml version="1.0" encoding="GBK"?>
<ehcache>

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="false"
        />
</ehcache>

示例类及类的配置文件:

package com.pengsuen.bean;

public class News {
	 private int id;
	 private String title;
	 private String conment;
	 
	public News(){}
	public News(String title,String conment){
		this.title=title;
		this.conment=conment;
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getConment() {
		return conment;
	}
	public void setConment(String conment) {
		this.conment = conment;
	}
	
}

News.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.pengsuen.bean.News" table="NEWS">
    	<cache usage="read-only"/>
        <id name="id" type="int">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" />
        </property>
        <property name="conment" type="java.lang.String">
            <column name="conment" />
        </property>
    </class>
</hibernate-mapping>

主配置文件略


测试类:

package com.pengsuen.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.pengsuen.bean.News;

public class Test {
	public static void main(String[] args) {
		// 默认找hibernate.cfg.xml
		Configuration conf = new Configuration().configure();
		SessionFactory sf = conf.buildSessionFactory();
		
		News news1= new News("success","ok");
		News news2= new News("fuck","ko");
		
		Session session0 = sf.openSession();
		session0.beginTransaction();
		session0.save(news1);
		session0.save(news2);
		
		List names = session0.createQuery("from News news").list();
		session0.getTransaction().commit();
		System.out.println("-------------------------");
		
		Session session2 = sf.openSession();
		session2.beginTransaction();
		News user = (News)session2.load(News.class, 2);
		System.out.println(user.getTitle() + " " + user.getConment());
		session2.getTransaction().commit();
		

		session0.close();
		session2.close();
		sf.close();


	}
}
执行结果: