Home » Hibernate » Object States in Hibernate – Transient,Persistent and Detached

Object States in Hibernate – Transient,Persistent and Detached

Object states in Hibernate plays a vital role in the execution of code in an application. Hibernate has provided three different states for an object of a pojo class. These three states are also called as life cycle states of an object.

Hibernate Object States

There are three types of Hibernate object states.

1. Transient Object State:

An object which is not associated with hibernate session and does not represent a row in the database is considered as transient. It will be garbage collected if no other object refers to it.

An object that is created for the first time using the new() operator is in transient state. When the object is in transient sate then it will not contain any identifier (primary key value). You have to use save, persist or saveOrUpdate methods to persist the transient object.


Employee emp = new Employee();
emp.setName("Ravi Raj");
// here emp object is in a transient state

In the above java code emp is Employee object which is in transient state.

2. Persistent Object State:

An object that is associated with the hibernate session is called as Persistent object. When the object is in persistent state, then it represent one row of the database and consists of an identifier value.You can make a transient instance persistent by associating it with a Session.


Long id = (Long) session.save(emp);
// emp object is now in a persistent state

In above lines of code emp object is in transient state.

3. Detached Object State:

Object which is just removed from hibernate session is called as detached object.The sate of the detached object is called as detached state.

When the object is in detached sate then it contain identity but you can’t do persistence operation with that identity.

Any changes made to the detached objects are not saved to the database. The detached object can be reattached to the new session and save to the database using update, saveOrUpdate and merge methods.


session.close();
//object in detached state

In above code since session is closed so emp object currect status is detached.

Example


package com.jwt.hibernate.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.jwt.hibernate.Student;

public class ObjectStatesDemo {
	public static void main(String[] args) {

		// Transient object state
		Student student = new Student();
		student.setId(101);
		student.setName("Mukesh");
		student.setRoll("10");
		student.setDegree("B.E");
		student.setPhone("9999");
		// Transient object state
		Session session = new AnnotationConfiguration().configure()
				.buildSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		// Persistent object state
		session.save(student);
		t.commit();
		// Persistent object state
		session.close();
		// Detached object state

	}
}

Output :


Hibernate: 
    insert 
    into
        STUDENT
        (degree, name, phone, roll, id) 
    values
        (?, ?, ?, ?, ?)

In The Database :


mysql> select * from student;
+-----+--------+--------+-------+------+
| id  | degree | name   | phone | roll |
+-----+--------+--------+-------+------+
| 101 | B.E    | Mukesh | 9999  | 10   |
+-----+--------+--------+-------+------+
1 row in set (0.05 sec)

Previous Next Article

comments powered by Disqus