Lifecycle Of POJO Class Objects




Friend, our POJO class object having 3 states
Transient state
Persistent state
Detached state



Transient & Persistent states:
Whenever an object of a POJO class is created then it will be in the Transient state.



An object in a Transient state doesn’t represent any row of the database,



Means it is not associated with any Session object, No relation with the database, it is just a
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,



In persistent state object is having associated with the unique Session



if we want to move an object from persistent to detached state, then we need to do either
close 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.


HibernateEngine.java
package javafiles;



import org.hibernate.*;
import org.hibernate.cfg.*;



public class HibernateEngine {



public static void main(String[] javaPlanet)
{



System.out.println("********START *********");
Configuration configurationObj = new Configuration();
configurationObj.configure("hibernate.cfg.xml");
SessionFactory sessionFactoryObj =
configurationObj.buildSessionFactory();
Session sessionObj = sessionFactoryObj.openSession();



Student studObj=new Student();
/* object studObj is in transient state now */



studObj.setRollNumber(99);
studObj.setName("Akruti");
studObj.setAddress("Baroda");
Transaction transactionObj = sessionObj.beginTransaction();


sessionObj.save(studObj);
/* object studObj is in persistent state now */



System.out.println("Student Data saved successfully.");
transactionObj.commit();
sessionObj.close();
/* object studObj is in detached state now */



sessionFactoryObj.close();
System.out.println("****** END **********");
}
}



Read me :
See the above client program, first we just loaded the object and called the.

corresponding setter methods, it is not related to the database table row.


When we called save method in the Session Interface that means the object is now
relation with the database.



If we want to convert the object from Transient state to Persistent state we can do in two
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 , If we do the modifications 10 times, then 10 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.


Transient Object :
One newly created object of POJO class without having any relation with the database, means never persistent, not associated with any Session object.



Persistent Object:
An object of a POJO class having relation with the database, associated with a unique Session object



Detached Object:
An object of a POJO class Previously having relation with the database [persistent ], now not associated with any Session object