Life cycle of objects in Hibernate

hibernate_logo
In Hibernate, there are three states of any object as:

  1. Transient
  2. Persistent
  3. Detached

In any application with persistent state, the application must interact with the persistence layer i.e. DAO: Data Access Object or sometimes called DAL (Data Access Layer). In this post , I explain these three states: Transient, Persistent, and Detached.

1. Overview of different methods

This graph has taken from internet, to describ about how state of object goes from one state to another by calling a perticular method. You can find hihg level in fact low level overview by going though the method of Hibernate session.

Object life cycle in Hibrenate ORM

Above graph describes about how object goes from one state to another by calling of method on Hibernate session or Persistence manager.

1. Transient objects

In Hibernate, objects instantiated using the new operator (e.g. new Employee("ranjeet", 2); ) aren’t immediately goes into persistent state. Transient state means object aren’t associated with any database table or row. When associated with hibernate session then it goes to Persistent state, it means before association of Hibernate’s session. secondly, when it deattached from Hibernate Session, it goes again to Transient state.

Hibernate considers all transient instances to be nontransactional; a modification to the state of a transient instance isn’t made in the context of any transaction. This means Hibernate doesn’t provide any rollback functionality for transient objects. For an instance to transition from transient to persistent state requires either a save() call to the persistence manager or the creation of a reference from an already persistent instance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 public int saveEmployee(Employee employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    int empId = 0;
    try {
      transaction = session.beginTransaction();
      empId = (Integer) session.save(employee);
      transaction.commit();
    } catch (HibernateException e) {
      transaction.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }
    return empId;
  }
Note: when line no 7 executes , you can say its in persistent state and when this method returns it goes in dettached state.

Persistent objects

When it attached with Hibernate session means object is in persistent state. Persistent instances are then associated with the persistence manager. Persistent instances participate in transactions—their state is synchronized with the database at the end of the transaction. When a transaction commits, state held in-memory is propagated to the database by the execution of SQL INSERT, UPDATE, and DELETE statements.

Detached objects

When a transaction completes, the persistent instances associated with the persistence manager still exist. If the transaction were successful, their in-memory state will have been synchronized with the database. In the case of Hibernate, however, these instances lose their association with the persistence manager when you close() the Session. We refer to these objects as detached, meaning that their state is no longer guaranteed to be synchronized with database state; they’re no longer under session of Hibernate. You can say when method return entity associated with session , will have to goe in detached state.

Your comments are welcome to improve this post of Hibernate object state life cycle. I hope you enjoyed by reading this article. Happy Learning :)

Please follow and like us:
RSS
Follow by Email
Facebook
LinkedIn
Google+

Leave a Reply

Your email address will not be published. Required fields are marked *