What is the difference between transient, persistent and detached object in Hibernate?
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