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

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 XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Hibernate save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 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 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 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 Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

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 Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

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.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.