Hibernate一级缓存

Session缓存:
  • 在Session接口的实现中包含了一系列Java集合,这些Java集合构成了Session缓存,只要Session实例没有结束生命周期,且没有清理缓存,则存放在它缓存中的对象也不会结束生命周期。
  • Session缓存可以减少Hibernate应用程序访问数据库的频率。在这段测试代码里面   只访问了数据库一次,第二次是通过缓存访问的。
  • News对象有三个引用: Session缓存, news1, news2.
 
操作Session缓存:
  • flsush(): 当session缓存中的对象发生变化时,会自动调用flush(), 最终会调用一条SQL语句(update/insert/delete)来实现session缓存和数据库的同步。
 

flush()是按照数据库中缓存的变化来同步更新数据库
默认情况下session在以下情况下刷新缓存
  • 显示的调用flush()
  • 执行commit()
  • 当应用程序执行HQL Criteria查询时,如果缓存中持久化对象的属性发生了变化的话,会先flush缓存,以保证查询结果能够反映持久化对象的最新状态

flush()缓存的例外情况,如果对象使用native模式生成OID, 那么当调用Session的save()方法保存对象时,会立即执行向该数据库插入该实体的insert语句。


commit()和flush()方法的区别, flush()方法相当于执行了一系列SQL语句, 但是不提交事务, commit()方法先执行flush()方法,然后提交事务,意味着数据对象被永久的保存下来。



具体实现参考以下代码:
  • 需要加载的库:

  • 配置文件: 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="connection.username">root</property>
    <property name="connection.password">86915310</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/user</property>

    <!-- 配置hibernate的基本信息 -->
    <!-- hibernate所使用的数据库方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    
    <!-- 是否在控制台打印SQL -->
    <property name="show_sql">true</property>
    
    <!-- 是否对SQL进行格式化 -->
    <property name="format_sql">true</property>
    
    <!-- 指定自动生成数据表的策略 -->
    <property name="hbm2ddl.auto">update</property>
    
    <!-- 设置hibernate的事务隔离级别 -->
    <property name="connection.isolation">2</property>
    
    <!-- 指定关联的.hbm.xml文件 -->
    <mapping resource="hibernate/sessiondemo/News.hbm.xml"/> 
    </session-factory>
</hibernate-configuration>
  • 创建一个持久化类:News.java
package hibernate.sessiondemo;

import java.util.Date;

public class News {
	
	private Integer id;
	private String title;
	private String author;
	
	private Date date;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public News(String title, String author, Date date) {
		super();
		this.title = title;
		this.author = author;
		this.date = date;
	}

	public News() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
	}
}
  • 生成对象关系配置文件: 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">
<!-- Generated Feb 15, 2016 9:19:20 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="hibernate.sessiondemo.News" table="NEWS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        </property>
        <property name="author" type="java.lang.String">
            <column name="AUTHOR" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="DATE" />
        </property>
    </class>
</hibernate-mapping>
  •   编写测试类:HibernateTest.java
package hibernate.sessiondemo;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HibernateTest {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
 
	@Before
	public void init(){
		// 1. 创建一个 SessionFactory 对象
		StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder()
				.configure("hibernate.cfg.xml").build();

		Metadata metadata = new MetadataSources(standardServiceRegistry).addAnnotatedClass(News.class)
				.addAnnotatedClassName("hibernate.sessiondemo.News").addResource("/hibernate/sessiondemo/News.hbm.xml")
				.getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
				.build();

		sessionFactory = metadata.getSessionFactoryBuilder().build();

		// 2. 创建一个 Session对象
		session = sessionFactory.openSession();
		System.out.println(session);

		// 3. 开启事务
		transaction = session.beginTransaction();
	}
	
	@After
	public void destroy(){
		// 5. 提交事务
		transaction.commit();

		// 6. 关闭session
		session.close();

		// 7. 关闭sessionFactory对象
		sessionFactory.close();
	}
	/**
	 * clear(): 清理缓存
	 */
	@Test
	public void testClear(){
		News news1 = session.get(News.class, 1);
		System.out.println(news1);
		//本来只需要执行一次select语句,但是在执行完clear之后需要重新执行select语句
		session.clear();  
		News news2 = session.get(News.class, 1);
		System.out.println(news2);
	}
	
	/**
	 * refresh()会强制发送SELECT语句,以使Session缓存中对象的状态和数据表中的记录保持一致!
	 */
	@Test
    public void testRefresh(){
    	News news = (News) session.get(News.class, 1);
    	System.out.println(news);
    	session.refresh(news);
    	System.out.println(news);
    }
	
	@Test
	public void testSessionFlush2() {	
		News news = new News("LongDeChuanRen", "LiXiaoLong", new Date());
		session.save(news);	
		//session.flush()
		//System.out.println("flush");
	}
	
	@Test
	public void testSessionFlush() {	
		News news = (News) session.get(News.class, 1);
		news.setAuthor("aa");
		
		//session.flush()
		//System.out.println("flush");
	}
	
	@Test
	public void testSessionCache() {
	  /**
	   * 1. 在Session接口的实现中包含了一系列Java集合,这些Java集合构成了Session缓存
	   * 只要Session实例没有结束生命周期,且没有清理缓存,则存放在它缓存中的对象也不会结束生命周期。
	   * 
	   * 2. Session缓存可以减少Hibernate应用程序访问数据库的频率。在这段测试代码里面只访问了数据库一次,第二次是通过缓存访问的。
	   * 
	   * 3. News对象有三个引用: Session缓存, news1, news2.
	   */
	  News news1 = (News) session.get(News.class, 1);
	  System.out.println(news1);
	  
	  News news2 = (News) session.get(News.class, 1);
	  System.out.println(news2);
	}
}

  

在执行 referesh()函数的时候,要考虑数据库的隔离属性:有待学习。。。。

 


posted on 2016-02-15 18:21  我表情悠哉  阅读(217)  评论(0编辑  收藏  举报

导航