Hibernate Lifecycle Of pojo Class Objects

Actually our POJO class object having 3 states like…

  • Transient state
  • Persistent state
  • Detached state

 

Transient & Persistent states:

  • When ever an object of a pojo class is created then it will be in the Transient state
  • When the object is in a Transient state it doesn’t represent any row of the database, i mean not associated with any Session object, if we speak more we can say no relation with the database its just an normal object
  • If we modify the data of a pojo class object, when it is in transient state then it doesn’t effect on the database table
  • When the object is in persistent state, then it represent one row of the database, if the object is in persistent state then it is associated with the unique Session
  • if we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session
  • if we want to move an object from persistent state into transient state then we need to delete that object permanently from the database
  • Example____ ClientProgram.java

    123456789101112131415161718192021222324252627282930313233import 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(); } }

    Note:

  • see the above client program, line numbers 16 to 19 we just loaded the object and called the corresponding setter methods, its not related to the database row
  • if you see, line number 24 we called save method in the Session Interface, means the object is now having the relation with the database
  • if we want to convert the object from Transient state to Persistentstate we can do in 2 ways
    • By saving that object like above
    • By loading object from database
  • If we do any modifications all the changes will first applied to the object in session cache only (Let__ we do the modifications 5 times, then 5 times we need to save the changes into the database right, which means number of round trips from our application to database will be increased, Actually if we load an object from the database, first it will saves in the cache-memory so if we do any number of changes all will be effected at cache level only and finally we can call save or update method so with the single call of save or update method the data will be saved into the database.

    If we want to save an object into database then we need to call any one of the following 3 methods

  • save()
  • persist()
  • saveOrUpdate()
  • i will explain about persist, saveOrUpdate methods later….

    If we want to load an object from database, then we need to call either load() or get() methods

    Transient:

    One newly created object,with out having any relation with the database, means never persistent, not associated with any Session object

    Persistent:

    Having the relation with the database, associated with a unique Session object

    Detached:

    previously having relation with the database [persistent ], now not associated with any Session

    see the next sessions for the better understanding of the life cycle states of pojo class object(s) the hibernate

Related Articles

post a comment