Hibernate object states-

State | Definition | Example | Code Snippet |
---|---|---|---|
Transient | A 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 yet | Person p = new Person(); |
Persistent | A 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 session | Person p = session.get(Person.class, 1); |
Detached | When 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 closed | Person p = session.get(Person.class, 1); <br> session.close(); <br> p.setName("John Doe"); |