Hibernate Lifecycle

Hibernate Lifecycle

In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do not have a lifecycle, only the entity object do. Hibernate prefers its object to be in a particular “state,” and there are mainly four states of the object.

Following are the states of the object's lifecycle:

  • Transient State
  • Persistent State
  • Detached State
  • Removed State
Hibernate Lifecycle

Transient State

An object is said to be transient, if it is initialized using the new operator, and it is not associated with any Hibernate Session. As it is not associated with any Session, it doesn’t have any representation in the database table. If we made changes to the data of the POJO class, it would not affect the database.

For example:

There exists a POJO class called Payment.java. We are creating a new object for the Payment class. Following code shows the initialization of the Payment object:

Payment pay = new Payment();     //The object pay is in transient state

To make an object persistent, we use Hibernate Session. A transient object may be made persistent by calling save(), persist() or saveOrUpdate() functions.

Persistent State

A persistent object has a representation in the database, an identifier value, and it is also associated with a Hibernate Session. In this state, Hibernate will detect changes made to an object and synchronize it with the database. If we made changes in the data, it would also affect the database. It is also present in a heap.

We can create a persistent object by calling session.save(), session.persist(), session.update(), session.saveOrUpdate(), session.lock() and session.merge().

Following code shows the transformation of a transient object into persistent.

Payment pay = new Payment();     //The object pay is in transient state
Session s = sessionfactory.openSession();
pay.setId(01);
session.save(pay)                   //The object pay  is in persistent state. 

A persistent object may be made transient by calling session.delete() function.

Detached State

When the session of a persistent object has been closed, the object will be known as a detached object. As a detached object is no more associated with the Session, changes made to the data will not affect the database. The detached object can be reattached to a new session when needed.

We can create a detached object by calling session.close(), session.clear(), session.detach() and session.evict().

Following code shows the initialization of detached object:

Payment pay = new Payment();     //The object pay is in transient state
 Session s = sessionfactory.openSession();
 pay.setId(01);
 session.save(pay)                   //The object pay  is in persistent state.
 session.close(pay):               //The object pay is in detached state. 

+

Removed State

An object is said to be in the removed state when it is deleted from the database. In this state, the object may still exist, but Hibernate will ignore it. Any changes made to the data will not affect the database.

 We can create a removed object by calling session.delete().

Following code shows the initialization of removed object:

Payment pay = new Payment();     //The object pay is in transient state
Session s = sessionfactory.openSession();
pay.setId(01);
session.save(pay)                   //The object pay  is in persistent state. 
session.delete(pay);             //The object pay is in removed state. 


Related Topics

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

2 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.