Cascade & Orphan Removal

Sungju Kim·2024년 9월 8일

Sparta_Coding_Camp_TIL

목록 보기
32/53

In JPA (Java Persistence API), cascade and orphan removal options control how changes in an entity’s lifecycle (like persist, remove, etc.) affect related entities. These options are particularly useful when managing associations between entities, such as OneToMany, ManyToOne, ManyToMany, or OneToOne relationships.

Cascade Types:

The cascade option specifies which operations on an entity should be automatically propagated to its associated entities. These are the common cascade types:

  1. CascadeType.PERSIST: When the parent entity is persisted (saved), all related entities are also persisted.

    • Example: When you save a Post, the Likes related to that Post will also be saved.
  2. CascadeType.MERGE: When the parent entity is merged (updated), all related entities are also merged.

    • Example: If you update a Post, any changes to the related Likes are also updated.
  3. CascadeType.REMOVE: When the parent entity is removed (deleted), all related entities are also removed.

    • Example: If you delete a Post, all Likes associated with the Post will also be deleted.
  4. CascadeType.REFRESH: If the parent entity is refreshed from the database, all associated entities are also refreshed.

    • Example: Reloading a Post will also reload its related Likes.
  5. CascadeType.DETACH: When the parent entity is detached from the persistence context, all associated entities are also detached.

    • Example: If you detach a Post, the related Likes are no longer managed by the entity manager.
  6. CascadeType.ALL: This is shorthand to apply all of the above cascade operations.

Orphan Removal:

The orphanRemoval option is used to automatically remove child entities when they are no longer associated with the parent entity.

  • orphanRemoval = true: If a child entity (e.g., a Like) is removed from its parent’s collection (e.g., the list of Likes in a Post), it is automatically deleted from the database.

  • orphanRemoval = false: The child entity is simply disassociated from the parent without being deleted from the database.

Differences between Cascade and Orphan Removal:

  • Cascade focuses on propagating operations (persist, remove, etc.) from parent to child.
  • Orphan removal deals specifically with removing entities that are no longer referenced by their parent.

Example:

@Entity
public class Post {
    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Like> likes;
}

In this example:

  • If you persist or remove a Post, the operations will cascade to the Like entities.
  • If you remove a Like from the likes collection of the Post, that Like will be deleted from the database if orphanRemoval = true.

💡What is the difference between CascadeType.REMOVE an Orphan Removal?💡

FeatureCascadeType.REMOVEorphanRemoval = true
Triggered whenParent entity is deletedChild is disassociated from the parent (removed from the collection or reference set to null)
Parent entity removalRequiredNot required
Use caseFor cascading deletes when the parent is removedTo automatically delete orphaned child entities that are no longer referenced by the parent
Applicable toAll cascade types (including REMOVE)Only applies to disassociated (orphaned) children
Common usageManaging child entities that should be deleted when the parent is deletedManaging child entities that should be deleted when no longer referenced by the parent

Example Scenario:

Suppose you have a Parent entity that contains a collection of Child entities:

Parent parent = entityManager.find(Parent.class, parentId);
parent.getChildren().remove(child);  // Removing a child from the collection
entityManager.persist(parent);  // Save the parent
  • If orphanRemoval = true, the child that was removed from the collection will be deleted from the database.
  • If only CascadeType.REMOVE is used, the child will not be deleted unless the parent itself is deleted using entityManager.remove(parent).

Summary:

  • CascadeType.REMOVE: Child entities are deleted only when the parent entity is explicitly deleted.
  • orphanRemoval = true: Child entities are deleted when they are no longer referenced by the parent entity, even if the parent is not deleted.

Use orphanRemoval when you want to automatically delete child entities that are removed from the parent’s collection, and use CascadeType.REMOVE when you want the child entities to be deleted only when the parent is deleted.

profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글