Cascade Types

Cascade Types

What is Cascading?

Cascading plays an important role in the case of assosiation mapping. With cascading, we can make sure that any state change, made in the parent can also affect the child state in the parent-child mapping.



JPA Cascade Types

  • ALL
  • PERSIST
  • MERGE
  • REMOVE
  • REFRESH
  • DETACH

CascadeType.ALL

The persistence will propagate all EntityManager operations (PERSIST, REMOVE, REFRESH, MERGE, DETACH) to the related entities.Entity relationships often depend on the existence of another entity

Ex: the Employee–Employee user account relationship. Without the employee, the User Account entity doesn't have any meaning of its own. When we delete the Employee entity, our User Account entity should also get deleted.


CascadeType.PERSIST

The cascade persist is used to specify that if an entity is persisted then all its associated child entities will also be persisted

Ex: when we saved the Employee entity, the User Account entity will also get saved.



CascadeType.MERGE

Merge is used to update an existing record in the database with a unique identifier. First, the record is loaded from the database in memory and its properties are changed. when the merge operation is fired, the changes are updated in the database.



Cascade.Type.REMOVE
It means that the related entities are deleted when the owning entity is deleted.

Ex: When the employee deleted, the User Account will also be deleted.



Cascade.Type.REFRESH

The refresh operation will discard the current entity state in memory. Any changes made to an entity in memory can be overridden by database value.



Cascade.Type.DETACH

DETACH plays the role when more than one entity is associated to each other. If one entity is detached, other associated entities will also be detached.



Post a Comment

0 Comments