Hibernate : Object states and lifecycle

An object is in transient state when it is in heap but not persisted or yet not been given to the hibernate session for persistence.

Once the object/entity is inatilized and exists in memory, however it is not handed over to hibernate, it is in transient state.
When object is being handled or tracked by hibernate session or is in control of hibernate it is called as persistent state. Object in this state remains in synch with the database and is managed by the persistence manager.
After the session is close or once hibernate session leaves the control of object it is called as detached state.

Persistent objects become Detached objects when the session is closed. Calling session.evict() on the object
reference or session.clear() will also remove an object from session. These later session operations will be
covered later in class when cache is discussed.


Detached objects can be reassociated and synchronized to the database after a new session is opened. Calling
session.update() or session.saveOrUpdate() on a Detached object reference transitions the Detached object
back to the Persistent state. When a Detached object is “reattached,” the database is updated with object’s
current state; to include changes made while detached.

A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.
Hence once the object is persisted, it transition is mainly in between two states detached and attached (persistent) in its life cycle. It can also come back to transient state only if call delete() in session.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.