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. LOCK
The CascadeType.ALL is used to propagate all operations (save, persist, merge, refresh, etc.) from a superclass to a subclass.
SYNTAX of CascadeType.ALL
The CascadeType.DELETE corresponds to the Hibernate native Delete operation.
SYNTAX of CascadeType.DELETE
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
The CascadeType.LOCK re-attaches the removed entity and its mapped subclass entity with the persistent state again.
SYNTAX of CascadeType.LOCK
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
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
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
The CascadeType.REMOVE is used to delete the entity from the database. It is very similar to CascadeType.DELETE.
SYNTAX of CascadeType.REMOVE
The CascadeType.REPLICATE is used to sync data when there are more than one data sources.
SYNTAX of CascadeType.REPLICATE
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