jobs4timesLogo jobs4timesLogo

Hibernate

Agenda

Life cycle of POJO class :

Our POJO class have 3 states.
  1. Transient state
  2. Persistent state
  3. Detached state
1. Transient state
A newly created POJO class object is said to be in Transient state.
  • A Transient state object is not associated with the session.
  • Whenever POJO class object is in Transient state, it doesn't represent any record in the database.
  • Any modifications done to the Transient state object doesn't reflect in the database.

We have 2 approaches to move Transient state object to Persistent state object.

  1. Save the Transient state object by calling either save() or persist() method.
  2. Load the object from the database by calling either load() or get() metod.
2. Persistent state
  • Persistent state object is associated with the session. Whenever POJO class object is in Persistent state, then it represents one record in the database.
  • Whenever POJO class object is in Persistent state , then object is directly synchronized with the database record.
  • Any modifications done to the object will be directly reflect to the record in the database whenever we commit the transaction.
  • To move Persistent state object to Detached state, we need to remove the object from the session scope by calling evict() method, or we have to clear or close the session by calling either clear() or close() method.
  • To move Persistent state object to Persistent state, we need to delete the record from the database by calling delete() method.
3. Detached state
  • Detached state object is not associated with the session but it represents the record in the database.
  • To move the Detached state object to Persistent state, we need to call update() method.

Note : POJO class, Mapping and Configuration files are same as above application.

Client Application 1 :
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Employee;

public class ClientAppOne {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

// transient state
Employee e=new Employee();
e.setEmployeeId(2l);
e.setEmployeeName("charan");

// persistent state
session.save(e);
tx.commit();

System.out.println("---------");

}
}
Client Application 2 :
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Employee;

public class ClientAppTwo {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

//persistent state by loading object
Employee e=(Employee)session.get(Employee.class, 2l);
System.out.println(e.getEmployeeName());

//modifying employeeName
e.setEmployeeName("Akshay");

//not required to call update() method explicitly
// i.e., session.update(e);
tx.commit();

System.out.println("update");

}
}

Client Application 3 :
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Employee;

public class ClientAppTwo {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

//persistent state by loading object
Employee e=(Employee)session.get(Employee.class, 2l);
System.out.println("Object is in Transient state");

//detached state
session.evict(e);
System.out.println("Object is moved to detached state");

//modifying employeeName
e.setEmployeeName("Arun");

/* Modifications will not reflect with the database
 *  because Object is in Detached state. 
 * If we want to save modified data to the database
 *  then we need to call update() explicitly
 */

//session.update(e);
tx.commit();

System.out.println("update");

}
}
Client Application 4 :
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Employee;

public class ClientAppTwo {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

//persistent state by loading object
Employee e=(Employee)session.get(Employee.class, 2l);

//transient state
session.delete(e);
tx.commit();

System.out.println("delete");

}
}