Hibernate Life Cycle

This tutorial contains full description of Hibernate life cycle.

Ads

Tutorials   
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 ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment
Ads

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.
session2.update(employee);

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


Removed State :

When the persistent object is deleted from the database ,it is reached into the removed state.
session.delete()
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

Advertisements

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

Advertisements

 

Discuss: Hibernate Life Cycle  

Post your Comment


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

Ads

 

Ads