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.
The cascade option specifies which operations on an entity should be automatically propagated to its associated entities. These are the common cascade types:
CascadeType.PERSIST: When the parent entity is persisted (saved), all related entities are also persisted.
Post, the Likes related to that Post will also be saved.CascadeType.MERGE: When the parent entity is merged (updated), all related entities are also merged.
Post, any changes to the related Likes are also updated.CascadeType.REMOVE: When the parent entity is removed (deleted), all related entities are also removed.
Post, all Likes associated with the Post will also be deleted.CascadeType.REFRESH: If the parent entity is refreshed from the database, all associated entities are also refreshed.
Post will also reload its related Likes.CascadeType.DETACH: When the parent entity is detached from the persistence context, all associated entities are also detached.
Post, the related Likes are no longer managed by the entity manager.CascadeType.ALL: This is shorthand to apply all of the above cascade operations.
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.
@Entity
public class Post {
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Like> likes;
}
In this example:
Post, the operations will cascade to the Like entities.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?💡
| Feature | CascadeType.REMOVE | orphanRemoval = true |
|---|---|---|
| Triggered when | Parent entity is deleted | Child is disassociated from the parent (removed from the collection or reference set to null) |
| Parent entity removal | Required | Not required |
| Use case | For cascading deletes when the parent is removed | To automatically delete orphaned child entities that are no longer referenced by the parent |
| Applicable to | All cascade types (including REMOVE) | Only applies to disassociated (orphaned) children |
| Common usage | Managing child entities that should be deleted when the parent is deleted | Managing child entities that should be deleted when no longer referenced by the parent |
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
orphanRemoval = true, the child that was removed from the collection will be deleted from the database.CascadeType.REMOVE is used, the child will not be deleted unless the parent itself is deleted using entityManager.remove(parent).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.