The orphanRemoval attribute in JPA tells the persistence provider to delete a child entity from the database when it is removed from its parent's relationship, or when the parent is deleted. It is available on @OneToOne and @OneToMany and is meant for compositions, where the child has no meaning without its parent, such as order lines in an order.
Unlike a database ON DELETE CASCADE, which reacts to deleting the parent row, orphanRemoval also reacts to the parent merely dropping the reference while it stays in the database.
An orphan is a child entity that was previously referenced by a parent and no longer is. With orphanRemoval = true, the provider schedules a DELETE for that child at flush time. The attribute is defined by Jakarta Persistence (jakarta.persistence; javax.persistence before Jakarta EE 9), and Hibernate implements it.
Removing a line from the collection inside a transaction is enough to delete its row:
At commit, Hibernate flushes and issues:
No call to remove() or repository.delete() is needed. Clearing the whole collection with order.getLines().clear() deletes every child. For a @OneToOne, setting the reference to null (or replacing it with another entity) deletes the old child.
The two settings overlap but are not the same:
| Situation | cascade = REMOVE | orphanRemoval = true |
|---|---|---|
| Parent entity is deleted | Children deleted | Children deleted |
| Child removed from the parent's collection | Child kept | Child deleted |
Parent reference set to null (@OneToOne) | Child kept | Child deleted |
Setting orphanRemoval = true implies the remove cascade for the parent-delete case, as defined in the specification, so you do not need REMOVE separately. A typical composition uses cascade = CascadeType.ALL, orphanRemoval = true so that saves, updates and deletions all flow from the parent.
order.setLines(new ArrayList<>()) on a managed entity makes Hibernate throw "A collection with cascade=all-delete-orphan was no longer referenced by the owning entity instance". Mutate the existing collection with clear() and addAll() instead.remove() on the collection. Queries executed in the same transaction trigger an auto flush, so deleted rows disappear from results.@OneToOne child with a new one that has the same unique value, Hibernate may insert before it deletes and violate the constraint. Flush between the two operations or avoid reusing the unique value.@SQLDelete or @SoftDelete, orphan removal runs your soft delete, so make sure that is intended.Orphan removal loads the collection so it can detect removals, and deletes children one statement at a time. For parents with thousands of children, a bulk JPQL delete from OrderLine l where l.order.id = :id is much faster, but it bypasses the persistence context, so clear or refresh affected entities afterwards.
Yes. Orphan removal is independent from the cascade setting for removal of a dereferenced child, although most compositions use both together.
@OneToOne and @OneToMany only. It is not available on @ManyToOne or @ManyToMany, because those children are not exclusively owned.
ON DELETE CASCADE is a database foreign key rule triggered by deleting the parent row. orphanRemoval is a JPA feature triggered by the application removing the child from the parent, and it also fires when the parent is deleted through JPA.
You replaced the managed collection with a new instance. Change its contents in place instead.
orphanRemoval = true makes a JPA child's lifecycle depend on its parent's reference to it: remove it from the parent and it is deleted. Use it for owned, non-shared children, pair it with cascade = ALL, keep the collection instance intact, and watch the delete behaviour and statement count on large collections.