We know that Hibernate works with plain Java objects (POJO). In raw form (without hibernate specific annotations), hibernate will not be able to identify these java classes. But when these POJOs are properly annotated with required annotations then hibernate will be able to identify them and then work with them e.g. store in the database, update them, etc. These POJOs are said to mapped with hibernate.
1. Entity Lifecycle States
Given an instance of a class that is mapped to Hibernate, it can be in any one of four different persistence states (known as hibernate entity lifecyle states):
Transient entities exist in heap memory as normal Java objects. Hibernate does not manage transient entities or persist changes done on them.
To persist the changes to a transient entity, we would have to ask the hibernate session to save the transient object to the database, at which point Hibernate assigns the object an identifier and marks the object as being in persistent state.
Persistent entities exist in the database, and Hibernate manages the persistence for persistent objects.
If fields or properties change on a persistent object, Hibernate will keep the database representation up to date when the application marks the changes as to be committed.
Detached entities have a representation in the database, but changes to the entity will not be reflected in the database, and vice-versa. This temporary separation of the entity and the database is shown in the image below.
A detached entity can be created by closing the session that it was associated with, or by evicting it from the session with a call to the session’s
In order to persist changes made to a detached object, the application must re-attach it to a valid Hibernate session. A detached instance can be associated with a new Hibernate session when your application calls one of the
save() methods on the new session with a reference to the detached object.
After the method call, the detached entity would be a persistent entity managed by the new Hibernate session.
Removed entities are objects that are being managed by Hibernate (persistent entities, in other words) that have been passed to the session’s
When the application marks the changes held in the session as to be committed, the entries in the database that correspond to removed entities are deleted.
Now let’s not note down the take-aways from this tutorial.
- Newly created POJO object will be in the transient state. Transient entity doesn’t represent any row of the database i.e. not associated with any session object. It’s plain simple java object.
- Persistent entity represents one row of the database and always associated with some unique hibernate session. Changes to persistent objects are tracked by hibernate and are saved into database when commit call happen.
- Detached entities are those who were once persistent in the past, and now they are no longer persistent. To persist changes done in detached objects, you must re-attach them to hibernate session.
- Removed entities are persistent objects that have been passed to the session’s
remove()method and soon will be deleted as soon as changes held in the session will be committed to database.
Happy Learning !!