Here we will learn about Hibernate Lifecycle or in other words, we can say that we will learn about the lifecycle of the mapped instances of the entity/object classes in hibernate. In Hibernate, we can either create a new object of an entity and store it into the database, or we can fetch the existing data of an entity from the database. These entity is connected with the lifecycle and each object of entity passes through the various stages of the lifecycle.
There are mainly four states of the Hibernate Lifecycle :
- Transient State
- Persistent State
- Detached State
- Removed State
As depicted from the above media one can co-relate how they are plotted in order to plot better in our mind. Now we will be discussing the states to better interpret hibernate lifecycle. It is as follows:
State 1: Transient State
The transient state is the first state of an entity object. When we instantiate an object of a POJO class using the new operator then the object is in the transient state. This object is not connected with any hibernate session. As it is not connected to any Hibernate Session, So this state is not connected to any database table. So, if we make any changes in the data of the POJO Class then the database table is not altered. Transient objects are independent of Hibernate, and they exist in the heap memory.
There are two layouts in which transient state will occur as follows:
- When objects are generated by an application but are not connected to any session.
- The objects are generated by a closed session.
Here, we are creating a new object for the Employee class. Below is the code which shows the initialization of the Employee object :
//Here, The object arrives in the transient state. Employee e = new Employee(); e.setId(21); e.setFirstName("Neha"); e.setMiddleName("Shri"); e.setLastName("Rudra");
State 2: Persistent State
Once the object is connected with the Hibernate Session then the object moves into the Persistent State. So, there are two ways to convert the Transient State to the Persistent State :
- Using the hibernated session, save the entity object into the database table.
- Using the hibernated session, load the entity object into the database table.
In this state. each object represents one row in the database table. Therefore, if we make any changes in the data then hibernate will detect these changes and make changes in the database table.
Following are the methods given for the persistent state:
// Transient State Employee e = new Employee("Neha Shri Rudra", 21, 180103); //Persistent State session.save(e);
State 3: Detached State
For converting an object from Persistent State to Detached State, we either have to close the session or we have to clear its cache. As the session is closed here or the cache is cleared, then any changes made to the data will not affect the database table. Whenever needed, the detached object can be reconnected to a new hibernate session. To reconnect the detached object to a new hibernate session, we will use the following methods as follows:
Following are the methods used for the detached state :
// Transient State Employee e = new Employee("Neha Shri Rudra", 21, 180103); // Persitent State session.save(e); // Detached State session.close();
State 4: Removed State
In the hibernate lifecycle it is the last state. In the removed state, when the entity object is deleted from the database then the entity object is known to be in the removed state. It is done by calling the delete() operation. As the entity object is in the removed state, if any change will be done in the data will not affect the database table.
Note: To make a removed entity object we will call session.delete().
// Java Pseudo code to Illustrate Remove State // Transient State Employee e = new Employee(); Session s = sessionfactory.openSession(); e.setId(01); // Persistent State session.save(e) // Removed State session.delete(e);
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.