Persistent Object Life Cycle

Persistent Object Life Cycle

In Hibernate, either we create an object of an entity and save it into the database, or we fetch the data of an entity from the database. Here, each entity is associated with the life cycle. The entity object passes through the different stages of the life cycle.

Persistent Object has three life cycle states

1. Transient state
2. Persistent state
3. Detached state

Capture
Capture

Note

  • Object is associated with session means object reference is available to the session object
  • Object present in the database means object identifier value is available in database primary key column (Non identifier column values are not matter)
Transient state

An object is said to be in transient state, when it is not associated with session and not present in data base.

Example: Table

Capture

Application code

Account account = new Account();
account.setAccountld(1003);
account.setName("Dillesh");
account.setBalance(1500);

In the above example account object is not associated with session and there is no matching record in the ACCOUNT table. So we can say that account object is in transient state. In this state object is non-transactional. i.e., object is not synchronized with record. i.e., Modifications done to entity, doesn’t save into database.

Persistent state

An object is said to be in persistence state, when it is associated with session as well as object present in database.

Example: Table

Capture

Application code

Account account = (Account) session.get(Account.class,1002);
  • In the above example account object is associated with session and there is a matching record in ACCOUNT table. So we can say that account object is in persistent state.
  • In this state object is transactional. i.e., the object is synchronized with database record.
  • Changes made to objects in a persistent state are automatically saved to the database without invoking session persistence methods
Detached state

An object is said to be in detached state, when the object is not associated with session but present in data base.

Example: Table

Capture

Application code 

Account account = new Account();
account.setAccountld(1004);
account.setName("cherry");
account.setBalance(2100);
//Now the account object is said to be in transient state
session.save(account);
// Now the account object is said to be in persistence state
session.close();
// Now the account object is said to be in detached state.

In the above example after calling session.close() method, account object is moved to Detached state from persistent state. As session i s garbage collected, if we try to perform some modifications to entity object those changes will not be stored into database.

Saving Changes to the Database

Session methods do NOT save changes to the database

  1. save();
  2. update();
  3. delete();

These methods actually SCHEDULE changes to be made to the database. Once Transaction committed, all the queries will be pushed to the database

session.getTransaction().commit();
Persistent Object Life Cycle
Scroll to top