Hibernate Object States

shape Description

Hibernate Object States is changed into 3 types in hibernate depending upon Session.

shape Conceptual
figure

Transient State

shape Description

When a POJO class Object is created or if null value is assigned to POJO class object then that object state is called as Transient State.

  • If an object contains Transient state, then it implies that there is no relation between POJO class Object to Database.
  • Even if the object is changed dynamically, there will be no effect on Database.

shape Example

 SPLessons splessons = new SPLessons();
       splessons.setId();
           (or)
   SPLessons splessons = null;
  // In both cases splessons object is Transient State.

Persistent State

shape Description

When a POJO class Object is attached to Cache of Session Object of Hibernate then that object state is Persistent State.

  • When an Object state is converted to Persistent State then relation will be established between POJO class Object and Database. If any modifications occurs on the object, then it will effect on Database.
  • When an Object State is converted from Transient State to Persistent State then saving/loading/storing is performed on that Object.

shape Example

For Ex:   Session session=new Configuration().configure().buildSessionFactory().openSession();
           SPLessons splessons=new SPLessons(); //splessons object is Transient State.
               splessons.setId(414);
          Transaction transaction=session.beginTransaction();
                  transaction.save(splessons); // splessons object is Persistent State.
                    splessons.setId(415);     //  In this time Id value is effected on DataBase.
                       transaction.commit();
                         

Detached State

shape Description

When POJO class Object comes out of the Session cache, Hibernate will call the object state into Detached State.

  • When Object state is Detached and if any changes are performed, it will not effect on the Database.
  • Object state is converted from Persistent state to Detached State by evicting (or) clearing the cache (or) closing a session.

shape Example

 transaction.commit();
       session.close(); // Object will goes to Detached State.         

shape Conceptual
figure

Here,

  • Object is converted from Detached State to Persistent State using update(), merge(), saveOrUpdate().
  • Object is converted from Persistent State to Transient State by using delete().
  • Object is changed to Transient State means the object is deleted from Cache and Database.

Difference between update() and merge()

shape Differences

While converting an Object from Detached State to Persistent State update() and merge() methods are used.

When using an update(), Hibernate first checks this object already exists or not in the cache.
If an Id given, already exists, then Hibernate will give the exception called NonUniqueObjectException. If the id doesn’t exist then it will update.

When called merge() and if Id doesn’t exist, then Hibernate will update() and if Id already exists in the Database, object state will be saved in database.

shape Example

<strong>ClientProgram.java</strong>
import org.hibernate.*;
import org.hibernate.cfg.*;
 
public class ClientProgram { 
 
	public static void main(String[] args)
	{
 
		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 
 
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
 
         // Transient state_____start
		Product p=new Product();
		p.setProductId(101);
		p.setProName("iPhone");
		p.setPrice(25000);
         // Transient state_____end
 
         // Persistent state_____start
		Transaction tx = session.beginTransaction();
		session.save(p);
		System.out.println("Object saved successfully.....!!");
		tx.commit();
         // Persistent state_____end  
 
		session.close();
		factory.close();
	}
 
}

see the above client program, where the developer just loaded the object and called the corresponding setter methods, its not related to the database row.

Product p=new Product();
		p.setProductId(101);
		p.setProName("iPhone");
		p.setPrice(25000);

Where the developer called save method in the Session Interface, means the object is now having the relation with the database.

session.save(p);

if one want to convert the object from Transient state to Persistent state one can do in 2 ways as follows.

  • By saving that object like above.
  • By loading object from database.
  • Summary

    shape Key Points

    • Hibernate Object States – If an object created to class, then that is in transient state.
    • Hibernate Object States – If an object represent one row in database then that is persistent state.
    • Hibernate Object States – Object will be deleted permanently from the database if an object moved from persistent state to transient state.