Step 4- States of objects in hibernate

Hibernate object states-

5
StateDefinitionExampleCode Snippet
TransientA transient object is an object that is not associated with a Hibernate session and has no representation in the database.A new instance of an entity class that has not been saved to the database yetPerson p = new Person();
PersistentA persistent object is an object that is associated with a Hibernate session and has a representation in the database.

Either saved in database
[using save, saveOrUpdate, persist, merge etc method]
or retrieved from the database [using get, load etc method]
An instance of an entity class that has been retrieved from the database using a Hibernate sessionPerson p = session.get(Person.class, 1);
DetachedWhen the session was closed, the state changes to detached.

or evict() , clear() etc methods are applied, The object is detached from its session.
An instance of an entity class that was retrieved from the database using a Hibernate session, but the session has been closedPerson p = session.get(Person.class, 1); <br> session.close(); <br> p.setName("John Doe");
Advertisement