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/ deleted, then the mapped entity associated with it should also be saved/ deleted automatically. Mostly cascading is used in Hibernate mapping strategies (one-to-one, many-to-one, etc.).

In other words, cascading is a way of using transitive persistence model. Cascading is all about the persistence actions involving the propagation of objects in the association mapping.

There are two ways to access cascade in Hibernate:

  • In XML file
  • In annotation

Cascade with an XML file

We can use cascade in an XML mapping file by declaring the cascade attribute. Following are the cascade types used in cascade attribute:

  • All ­­­­­­– It cascades all operations (save, persist, remove, lock, merge, etc.) It performs all cascade types that are listed below.
  • Delete /Remove – It is used to delete an object from the database. ­­­­
  • Detach – It is used to detach all the mapped entities if a manual-detach occurs. 
  • Merge – It is used to merge the mapped entities when the owning entities are merged.
  • Lock–It corresponds to the Hibernate native lock action.
  • Persist – It means that save() and persist() method cascades to the mapped entities.
  • Refresh – It is used to refresh the database tables.
  • Replicate – It is used to replicate the entity operation.
  • Save­_update /update – It is used to save or update the mapped entity.
  • None – It means No Cascading. When we don’t define any cascading type, then no operation in the superclass will affect the subclass.

Example of Cascade with an XML file

Following code is the example of cascading using XML file:

<set name="students" table="studenttab" inverse="true" lazy="true" fetch="select" cascade="save-update, delete">
             <key>         
                 <column name="Sdt_Id" not-null="true" />
             </key>
             <one-to-many class="Student" />
         </set>  

With Hibernate 5 (version 5 and above), cascade with annotation is used widely instead of using an XML file.

Cascade with Annotation

We can also use cascade with annotations. To access cascade, we need to declare @Cascade annotation in the POJO class. The @Cascade annotation contains a parameter called CascadeType, which defines the types of cascade strategy. The cascadeType is available in org.hibernate.annotation.Cascade package.

Following are the cascade types used in @Cascade annotation.

  • CascadeType.ALL
  • CascadeType.DELETE
  • CascadeType.DETACH
  • CascadeType. LOCK
  • CascadeType.MERGE
  • CascadeType.PERSIST
  • CascadeType.REFRESH
  • CascadeType.REMOVE
  • CascadeType.REPLICATE
  • CascadeType.SAVE_UPDATE

CascadeType.ALL

The CascadeType.ALL is used to propagate all operations (save, persist, merge, refresh, etc.) from a superclass to a subclass.

SYNTAX of CascadeType.ALL

@Cascade({CascadeType.ALL})

CascadeType.DELETE

The CascadeType.DELETE corresponds to the Hibernate native Delete operation.

SYNTAX of CascadeType.DELETE

@Cascade({CascadeType.DELETE})

CascadeType.DETACH

The CascadeType.DETACH removes the entity from the persistent state. When we use CascadeType.DETACH operation; the subclass object will get removed from the persistent state.

SYNTAX of CascadeType.DETACH

@Cascade({CascadeType.DETACH})

CascadeType.LOCK

The CascadeType.LOCK re-attaches the removed entity and its mapped subclass entity with the persistent state again.

SYNTAX of CascadeType.LOCK

@Cascade({CascadeType.LOCK})

CascadeType.MERGE

The CascadeType.MERGE copies the state of the given entity onto the persistent entity with the same primary key. It updates the database with the changes made by the persistent object.

SYNTAX of CascadeType.MERGE

@Cascade({CascadeType.MERGE})

CascadeType.PERSIST

The CascadeType.PERSIST makes the transient object persistent. If we save the superclass object, the subclass object will be saved automatically.

SYNTAX of CascadeType.PERSIST

@Cascade({CascadeType.PERSIST})

CascadeType.REFRESH

The CascadeType.REFRESH re-reads the entity value form the database. Whenever it reloads the superclass entity, the subclass entity is also gets reloaded automatically.

SYNTAX of CascadeType.REFRESH

@Cascade({CascadeType.REFRESH})

CascadeType.REMOVE

The CascadeType.REMOVE is used to delete the entity from the database. It is very similar to CascadeType.DELETE.

SYNTAX of CascadeType.REMOVE

@Cascade({CascadeType.REMOVE})

CascadeType.REPLICATE

The CascadeType.REPLICATE is used to sync data when there are more than one data sources.

SYNTAX of CascadeType.REPLICATE

@Cascade({CascadeType.REPLICATE})

CascadeType.SAVE_UPDATE

The CascadeType.SAVE_UPDATE propagates the same operation to the mapped subclass entity. It is used to save the entity into the database.

SYNTAX of CascadeType.SAVE_UPDATE

@Cascade({CascadeType.SAVE_UPDATE})

Related Topics

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 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 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 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 Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

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

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

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 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 GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how 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 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 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.

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