javaEE Hibernate, 缓存session, 快照

实体对象(JavaBean对象)的三种状态:

瞬时状态:没有id,没有在缓存session中
持久化状态:有id,在缓存session中
游离|托管状态:有id,没有在缓存session中

瞬时状态=>save=>根据主键生成策略生成id,并放到session缓存中=>持久化状态
瞬时状态=>setId()=>手动设置id=>游离|托管状态=>update()=>放到session缓存中=>持久化状态
持久化状态=>session.close()=>游离|托管状态
没有id(瞬时状态);有id(游离状态)=>saveOrUpdate()=>持久化状态
持久化状态=>提交事务=>真正保存到数据库中


Test.java:

package cn.xxx.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import cn.xxx.domain.Customer;
import cn.xxx.utils.HibernateUtils;

//测试一级缓存
public class Test {

	@Test
	//证明一级缓存存在
	public void fun1(){
		//1 获得session
		Session session = HibernateUtils.openSession();
		//2 控制事务
		Transaction tx = session.beginTransaction();
		//3执行操作
		
		Customer c1 = session.get(Customer.class, 1l);
		Customer c2 = session.get(Customer.class, 1l);
		Customer c3 = session.get(Customer.class, 1l);
		Customer c4 = session.get(Customer.class, 1l);
		Customer c5 = session.get(Customer.class, 1l); // 只执行了一次sql语句。 (优先从缓存session中获取)
		
		System.out.println(c3==c5); //true
		//4提交事务.关闭资源
		tx.commit();
		session.close();

	}
	
	@Test
	//
	public void fun2(){
		//1 获得session
		Session session = HibernateUtils.openSession();
		//2 控制事务
		Transaction tx = session.beginTransaction();
		//3执行操作
		Customer c1 = session.get(Customer.class, 1l); // 查询数据库,封装Customer对象,并保存到缓存(session)和快照中(持久化状态,有id)。(快照是为了判断数据是否进行过修改)
		
		c1.setCust_name("哈哈");  // 修改缓存中的对象。
		
		//4提交事务.关闭资源
		tx.commit();  // 将缓存中的对象与快照中的对象进行对比,如果不同(进行过修改)那么会将修改后的对象保存到数据库中。
		session.close();

	}
	
	@Test
	//持久化状态对象其实就是放入session缓存中的对象
	public void fun3(){
		//1 获得session
		Session session = HibernateUtils.openSession();
		//2 控制事务
		Transaction tx = session.beginTransaction();
		//3执行操作
		
		Customer c1 = new Customer(); // 瞬时状态(没有id,没有在session中)
		c1.setCust_id(1l); //托管|游离状态 (有id,没有在session中)
		
		session.update(c1); // 放入session缓存中。持久化状态(提交事务时,将持久化状态的对象同步更新到数据库)
		
		Customer c2 = session.get(Customer.class, 1l);  // 从缓存session中获取。
		
		//4提交事务.关闭资源
		tx.commit();
		session.close();// 游离|托管 状态, 有id , 没有关联
				
	}
}
HibernateUtils.java(Hibernate连接数据库的工具类):
package cn.xxx.utils;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtils {
	private static SessionFactory sf;  // 一个web中只需要一个SessionFactory对象。
	
	static{
		//1 创建Configuration对象,默认加载src下的hibernate.cfg.xml配置文件
		Configuration conf = new Configuration().configure();
		//2 根据配置信息,创建 SessionFactory对象
		 sf = conf.buildSessionFactory();
	}
	
	//获得session => 获得全新session
	public static Session openSession(){
		//3 获得session
		Session session = sf.openSession();
		return session;
	}
	
	//获得session => 获得与线程绑定的session(当前session)
	public static Session getCurrentSession(){
		//3 获得session
		Session session = sf.getCurrentSession();
		
		return session;
	}
}
 


猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/81053551
今日推荐