【Basic computer】-----Persist Object’s lifecycle in hibernate (Hibernate 中持久化对象的生命周期)



 Persist Object’s lifecycle in hibernate

                                                                                                         

                                                                                                     ------------ (Hibernate 中持久化对象的生命周期)



 
    There are three statement Persist Object’s lifecycle in hibernate. all this statement are connection with Session’s lifecycle ,you known why? Because session is the foundation of Hibernate’s action. there are contact each other.


 

 


【Transient】:

   Transient,we also call it free statement, this kind of state just inside the RAM,but there is no any database inside the DB. We usually create this kind of Object by method of “new”,this one can not persist and not inside the session which we define TransObject。
Java代码 
User user = new User();
	user.setName("Sarin");
	user.setCity("北京");
	user.setDepartment("大米时代");
	user.setPhone("15010422088");
	user.setHireTime(new java.util.Date());



【Persistent】:

     Persistent statement,it is totally different with Transient which connect with session and there have so many database inside the DB,this kind of statement save the object by Hibernate sentences ,statement like that which we define it persist object.
        SessionFactory sessionFactory = config.buildSessionFactory();
	Session session = sessionFactory.getCurrentSession();
	Transaction tx = session.beginTransaction();
	User user = new User();
	user.setName("Sarin");
	user.setCity("北京");
	user.setDepartment("大米时代");
	user.setPhone("15010422088");
	user.setHireTime(new java.util.Date());
	session.save(user);
	tx.commit();


【Detached】

     Detached statement: as you can see the picture, statement of Detached is island with session. Even though this kind of statement is already persist, but it is still not inside session cache which we define this kind of statement Detached Object.

SessionFactory sessionFactory = config.buildSessionFactory();
	Session session = sessionFactory.getCurrentSession();
	Transaction tx = session.beginTransaction();
	User user = new User();
	user.setName("Sarin");
	user.setCity("北京");
	user.setDepartment("大米时代");
	user.setPhone("15010422088");
	user.setHireTime(new java.util.Date());
	session.save(user);
	tx.commit();
	user.setCity("波士顿");


GC

        SessionFactory sessionFactory = config.buildSessionFactory();
	Session session = sessionFactory.getCurrentSession();
	Transaction tx = session.beginTransaction();
	User user = (User) session.get(User.class, new Integer(1));
	session.delete(user);
	tx.commit();



【Conclusion】

   

      Right here in this article I already give some simple introduction about the Persist Object’s lifecycle in hibernate. After the summary we can found that the Transient object and Detached Object are not inside of hibernate’s session, so ,no matter how many words or data you change in the object’s properties will not change the DB during this two statement.

While the data in DB will changing when session Object execute method of close() or Transaction Object execute  methofdof commit(). You know what , this is kind of a method to check the dirty data in hibernate .
  

     

      PS: this article is summary by Daniel after learned the video about Persist Object’s lifecycle in hibernate, if you have other good opinion or ideal please share with me I will be so thankful.

 





More in Chinese:   http://www.cnblogs.com/sunhan/p/3808680.html 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值