1. 程式人生 > >hibernate中的一級快取和二級快取

hibernate中的一級快取和二級快取

package test;

import org.hibernate.Session;
import com.wxh.dto.People;
import com.wxh.sessionfactory.HibernateSessionFactory;

public class Test {
	
	/**
	 * session.close()  
	 * 
	 * session中主要包括兩級快取
	 * 一級快取:session級別的快取,當第一次對指定資料發起查詢時,
	 * 系統會將查詢到的資料快取到session中;如果下一次的查詢是之前已經查詢過的物件,
	 * 則系統不會向資料庫發起查詢請求,而是直接到session中獲取快取的資料;如果session
	 * 執行過close,clear,evict的任意一個方法,則快取的資料會被清空,即使下一次查詢的資料
	 * 是已經查詢過的,系統依然會 向資料庫再次傳送查詢請求 
	 * 	  
	 * 二級快取:sessionFactory級別的快取,在配置的二級快取的前提下,即使session被關閉,
	 * 重新獲取新的session,新session依然能找到之前session快取的資料
	 * 二級快取的配置方式:
	 * 	1.匯入二級快取的外掛包hibernate/lib/optional/ehcache
	 *  2.在核心配置檔案中配置二級快取
	 *  <!-- 開啟二級快取 -->
	 *	<property name="cache.use_second_level_cache">true</property>
	 *  <!-- 設定二級快取實現類 5.0 -->
	 *  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
	 *  3.配置ehcache.xml(hiberate/project/etc/ehcache.xml)
	 *  4.設定對哪一個類的物件快取(兩種方案)
	 *      	方案一:在hibernate核心配置檔案中(配置如下:在mapping標籤的下方)
	 *      	<class-cache class="com.wxh.dto.People" usage="read-only"/>
	 * 			方案二:在對映檔案中的class標籤下,id標籤前設定
	 * 			<cache usage="read-only" region="sampleCache1"/>
	 * 				  在基於註解配置方式時,在class上加上
	 * 				 @Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="sampleCache1")
	 * 			推薦使用方案一 
	 * @param args
	 */
	

	public static void main(String[] args) {
		
		Session session=HibernateSessionFactory.getSession();
		
		session.beginTransaction();
		People p=session.get(People.class, 1);		
		session.getTransaction().commit();
		session.close();//清空session		
		System.out.println(p);
		//session.clear();//清空session
		//session.evict(p);//清空指定的物件
		session=HibernateSessionFactory.getSession();
		People p2=session.get(People.class, 1);
		System.out.println(p2);		
	}

}

People

package com.wxh.dto;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class People {	
	
	private Integer pid;	
	
	private String name;	
	private String sex;
	private String phone;
	private String address;	
	
	public People() {
	}	
	
	public People(String name, String sex, String phone, String address) {
		super();
		this.name = name;
		this.sex = sex;
		this.phone = phone;
		this.address = address;
	}

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,generator="mygen")
	@SequenceGenerator(name="mygen",sequenceName="auto_pid")
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "People [pid=" + pid + ", name=" + name + ", sex=" + sex
				+ ", phone=" + phone + ", address=" + address + "]";
	}
	
}

hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hbm2ddl.auto">update</property>
	<property name="dialect">
		org.hibernate.dialect.Oracle9Dialect
	</property>
	<property name="connection.url">
		jdbc:oracle:thin:@127.0.0.1:1521:orcl
	</property>
	<property name="connection.username">scott</property>
	<property name="connection.password">tiger</property>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="myeclipse.connection.profile">oracle</property>
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	
	<!-- 開啟二級快取 -->
	<property name="cache.use_second_level_cache">true</property>
	
	<!-- 設定二級快取實現類 5.0 -->
	<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
	
	
	<!-- 本地事務 -->
<!-- 	<property name="current_session_context_class">thread</property>		 -->
	<mapping class="com.wxh.dto.People"/>		
		
	<class-cache usage="read-only" class="com.wxh.dto.People"/>
		
	</session-factory>
</hibernate-configuration>