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>