What is the difference between transient, persistent and detached object in Hibernate?

What is the difference between transient, persistent and detached object in Hibernate?

Ninja Asked on 17th September 2018 in Hibernate.
Add Comment
1 Answer(s)
Best answer

An object is hibernate can exist in one of the three states of the life cycle – transient, persistent and detached.

When an object is instantiated, it is not associated with a session and it is not persisted directly in the database and it is said to be in transient state. When an object is associated with a session or when the transient object is made persistent, the object has a representation in the database and an identifier value and is said to be in persistent state.

When the session object is closed, the association is lost with the persistence manager and the object is said to be in detached state.

Student student = new Student();
student.setStudentId(1234);
student.setStudentName("Nitin");
// student object is in a transient state
Long id = (Long) session.save(student);
// session is an object of Hibernate Session
// student object is now in persistent state

....
session.close();
// student is now in detached state
Ninja Answered on 17th September 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.