Hibernate Life Cycle

This tutorial contains full description of Hibernate life cycle.


Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released

Hibernate Life Cycle

This tutorial contains full description of Hibernate life cycle.

Hibernate Object Life Cycle :

Hibernate Life cycle shows how your persistent object managed. Hibernate is ORM based java technology.
Hibernate saves, update and delete record in the database table regarding to value of object as your persistent class is associated with the database table.
Hibernate object life cycle contains four states. These are -

  1. Transient
  2. Persistent
  3. Removed
  4. Detached

Diagram :

For clearing the the flow and concept of hibernate life cycle ,here we taking example of employee table. Employee.java is our persistent class.

Transient State :

When we create an object of our pojo class ,we can say it is in Transient state.
By using new operator ,the object is instantiated and at that time it does not enter into persistent but
into transient state. At this state it is not related to any database table. At the time of Transient state
any modification does not affect database table. If no longer referenced by any other object, it refer to garbage collection.

Example :

 Employee employee=new Employee();    //employee is a transient object

Persistent State :

When you save your transient object it enter into persistent state. Persistent instance has valid database
table row with a primary key identifier .It is instantiated by the persistent manager when you call save().
If it is already exist you can retrieve it from the database. This is the state where objects are saved to the database.
All the changes done in this state, are saved automatically.
You can made object persistent by calling against Hibernate session -

  • session.save()
  • session.update()
  • session.saveOrUpdate()
  • session.lock()
  • session.merge()

Example :

Session session=Session.getCurrentSession();

Employee employee=new Employee() //transient state. Hibernate is unaware that it exist

session.saveOrUpdate(employee); //persistent state. Hibernate know the object and will save it to the database

employee.setName("Sonja"); //modification is saved automatically because the object is in the persistent state

session.getTransaction().commit(); //commit the transaction

Detached State :

In this state, the persistent object is still exist after closure of the active session.
You can say detached object remains after completion of transaction .It is still represents to the valid row in the database.
In detached state, changes done by detached objects are not saved to the database.
you can not detach your persistent object explicitly. evict() method is used to detach the object from session cache.
clear() ,close() methods of session can be used to detach the persistent object.

For returning in to the persistent state, you can reattach detached object by calling methods -

  • update()
  • merge()
  • saveOrUpdate()
  • lock() - It is reattached but not saved.

Example :

Session session1=Session.getCurrentSession();

Employee employee=Session1.get(Employee.class, 2); //retrieve employee having empId 2.employee return persistent object.

session1.close(); //transition is in detached state. Hibernate no longer manages the object.

employee.setName("Ron"); //Since it is in detached state so modification is ignored by Hibernate

Session session2=SessionFactory.getCurrentSession(); //reattach the object to an open session. Return persistent object and changes is saved to the database.

session.getTransaction.commit(); //commit the transaction

Removed State :

When the persistent object is deleted from the database ,it is reached into the removed state.
At this state java instance exist but any changes made to the object are not saved to the database.
It is ignored by Hibernate and when it is out of scope, it is assigned to garbage collection.

Example :

Session session=Session.getCurrentSession();

Employee employee=Session.get(Employee.class, 2); //retrieve employee having empId 2.employee return persistent object.

session.delete(employee); //transition is in removed state. database record is deleted by Hibernate and no longer manages the object.

employee.setName("Ron"); //Since it is in removed state so no modification done by Hibernate

session.getTransaction.commit(); //commit the transaction
Share on Google+Share on Google+

Hibernate Life Cycle

Posted on: July 23, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Life Cycle  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image