天天看点

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

 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.

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

【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.

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

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

继续阅读