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})