Site icon Narayana Tutorial

What are different states of hibernate entity bean?

Different ways to specify hibernate configuration property values

Hibernate Entity bean (Pojo) class having 3 states

1. Transient

2. Persistent

3. Detached

1. Transient:

Whenever pojo class object created then it will be in the Transient state.

Transient state Object does not represent any row of the database, It means not associated with any Session object or no relation with the database its just an normal object.

Transient state Object does not effect on the database table even if we modify the data of pojo class object.

Transient state Object may be made persistent by calling save(), persist() or saveOrUpdate().

2. Persistent:

Having the relation with the database, associated with a unique Session object

When an object is associated with a unique session, it’s in persistent state. it represent one row of the database, if the object is in persistentstate.

Any instance returned by a get() or load() method is persistent state object.

Example:

product p=(product)session.get(product.class);

product p =(product)session.get(product.class);

if we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session.

Example:

session.close();

session.clear();

3. Detached:

When an object is previously persistent but not associated with any session, it’s in detachedstate.

Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate().

The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().

Example:

session.close();

session.clear();