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

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.

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

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

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.

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

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.