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 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 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.

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 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 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 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 vs. JDBC

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...

2 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

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 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 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-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 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 Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 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.

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 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 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 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 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.