
CascadeType or Cascading is a feature of Hibernate to control the state mapped entity whenever the state of the owner class changes. When the owner class is saved or deleted, the entity associated with the owner class is also saved or deleted.
Cascade type is the most important feature of Hibernate which has shortened the code of save, update or delete. Cascade type is mostly used in one-to-one, many-to-one and other mapping techniques. Using cascading only makes sense if there is a parent-child relationship.
In simple words, if cascade is used and the parent/owner object is already persisted, and then we add the child object. In this situation, without calling save() or persist() method the child object will automatically become persistent.
Table of Contents
Cascade Enums
- PERSIST: The associated entity will be automatically persisted if the parent object persists.
- MERGE: The associated entity will be merged automatically when the parent object is merged.
- REMOVE: The associated entity will be removed automatically when the parent object is removed.
- REFRESH: The associated entity will be refreshed automatically when the parent object is refreshed.
- DETACH: The associated entity will be detached automatically when the parent object is detached.
- ALL: If you want to apply all the above cascade then you can use “All” type.
CascadeType.ALL = {PERSIST, MERGE, REMOVE, REFRESH, DETACH}
The property cascade = CascadeType.ALL indicates that all entities kept in this field will be persisted, removed, deleted or updated. You should use Cascade.ALL when you need to use all the cascade functionality.
Apart from the above Hibernate provides three more cascade types which are as follows.
- REPLICATE: The child will be replicated if the parent/owner class is duplicated.
- SAVE_UPDATE: If we use SAVE_UPDATE, persist and merge are both covered. Therefore, if we perform SAVE_UPDATE ON the parent/owner record then SAVE_UPDATE will also be applied on child records.
- LOCK
What is default CascadeType in JPA?
By default, None of the operation is cascaded. If you want the cascading then you will have to specify externally. There is no default cascade type in JPA and Hibernate.
Should we use cascading to child object?
Cascading is not very useful when applied to child. Just undesired code.
Where we can use cascade in hibernate?
If your table is connected to many persistent data tables, the data from that single table should cascade to another table that maintains dependent data. In that situation, CascadeType must be used.
Difference between CascadeType Persist and Merge
Persist: If you want to implement add functionality in your application then use persist() method. If you are sure that the object does not exist yet and if you want to ensure that only the new object is saved then it is advised to use the persist() method.
Merge: If you want to implement save functionality in your application then use persist() method. The merge() method is mostly used when we want to update the existing as well as persist the new object.
Recommended Articles
- What is hibernate in spring boot?
- Hibernate get() method Vs load() method
- Hibernate Interview Questions and Answers