hibernate之三种时态之间的转换

2021-03-02 00:00:00 转换 三种 时态

 

判断状态的标准

  oid  和    session相关性

public class HibernateUtils {
	private static SessionFactory sessionFactory=null;
	static{
		//获取config  加载配置文件
		Configuration configure = new Configuration().configure();
		sessionFactory = configure.buildSessionFactory();
	}
	//获取session
	public static Session getSession(){
		Session session = sessionFactory.openSession();
		return session;
	}
}

瞬时态

  瞬时态——持久态

  save和saveOrUpdata操作

	@Test//瞬时态转化为持久态
	public void test02(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		//处于游离态  没有oid  与session无关 
		User user=new User("zhangsan111","123456789","zhangfei","1064705527@qq.com","15029021367",new Date(),"M",1,"0");
		session.save(user);//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		ts.commit();
		session.close();
	}

  瞬时态——游离态  只需要给一个oid (主键生成策略进行了限定  这样我们只能通过持久态进行转换)

@Test//瞬时态转化为游离态态
	public void test02(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		//处于游离态  没有oid  与session无关 
		User user=new User("zhangsan111","123456789","zhangfei","1064705527@qq.com","15029021367",new Date(),"M",1,"0");
		session.save(user);//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		ts.commit();
		session.close();
		System.out.println(user.getUid());//处于游离态  有oid  session关闭
	}

持久态

  持久态—-瞬时态  delete操作

  

	@Test//持久态转化为瞬时态
	public void test03(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		User u=session.get(User.class,"402898a45fcec558015fcec55d5c0000");//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		System.out.println(u);
		session.delete(u);
		ts.commit();
		session.close();	
	}

  持久态—-游离态   close(关闭session)  clear 清除一级缓存(session)   evict清除以及缓存(指定的缓存) 

@Test//持久态转化为游离态
	public void test03(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		User u=session.get(User.class,"987e21997c134dc4b6538010d101bcef");//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		ts.commit();
		session.close();
		System.out.println(u);//此时为游离态  有oid  session关闭
	}

游离态

  游离态—-持久态

 

@Test//游离态转化为持久态
	public void test04(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		User u=session.get(User.class,"12");//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		ts.commit();
		session.close();
		u.setUsername("123456");//游离态
		Session session1 = HibernateUtils.getSession();
		//开启事务
		Transaction ts1 = session1.beginTransaction();
		session1.update(u);//持久态
		ts1.commit();
		session1.close();
	}

  游离态—-瞬时态

@Test//游离态转化为瞬时态
	public void test04(){
		//获取连接
		Session session = HibernateUtils.getSession();
		//开启事务
		Transaction ts = session.beginTransaction();
		User u=session.get(User.class,"12");//处于持久态  与session有关  有oid   (瞬时态转化为持久态)
		ts.commit();
		session.close();
		u.setUsername("123456");//游离态
		Session session1 = HibernateUtils.getSession();
		//开启事务
		Transaction ts1 = session1.beginTransaction();
		session1.delete(u);//瞬时态
		ts1.commit();
		session1.close();
	}

  

 

转载于:https://www.cnblogs.com/fjkgrbk/p/hibernate_state.html

    原文作者:weixin_34355881
    原文地址: https://blog.csdn.net/weixin_34355881/article/details/93640985
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章