What Is Cascading ?

Entity relationships often depend on the existence of another entity. Cascading is the way to achieve this. When we perform some action on the target entity, the same action will be applied to the associated entity.

JPA Cascade Type

All JPA-specific cascade operations are represented by the javax.persistence.CascadeType enum containing entries:

  • ALL : propagates all operations from a parent to a child entity
  • PERSIST : propagates the persist operation from a parent to a child entity
  • MERGE : propagates the merge operation from a parent to a child entity
  • REMOVE : propagates the remove operation from parent to child entity
  • REFRESH : the child entity also gets reloaded from the database whenever the parent entity is refreshed
  • DETACH : the child entity will also get removed from the persistent context

Hibernate Cascade Type

Hibernate supports three additional Cascade Types along with those specified by JPA. These Hibernate-specific Cascade Types are available in org.hibernate.annotations.CascadeType:

  • REPLICATE : a sync operation also propagates to child entities whenever performed on the parent entity
  • SAVE_UPDATE : propagates the same operation to the associated child entity
  • LOCK : re-attaches the entity and its associated child entity with the persistent context again.

References : Baeldung

Laisser un commentaire