Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between CascadeType.REMOVE and orphanRemoval = true on a @OneToMany collection?

CascadeType.REMOVE deletes the children when the parent itself is removed. orphanRemoval = true does that too, and also deletes a child when it is removed from the parent's collection or its link to the parent is cleared. Both work through the persistence context, loading the children and deleting them one by one, so we use them only for children owned exclusively by the parent. For big trees, a database ON DELETE CASCADE is much faster, but it bypasses JPA callbacks and leaves the persistence context unaware of the deleted rows.

Why this question is asked

These two settings look like synonyms, and many code bases set both without knowing what each adds. The difference shows up in everyday code: removing a line from an order's collection either deletes the row or leaves it behind, depending on one attribute. Interviewers ask this to check that we know exactly when each one deletes, how they do it, and when the database should do the work instead. In this answer, we will recap cascades, compare the two settings with code, look at how the deletes are executed, decide where they are appropriate, and compare them with ON DELETE CASCADE.

1A Quick Recap of Cascades

A cascade tells JPA to apply an operation on an entity to its associated entities as well. CascadeType.PERSIST saves new children when the parent is saved; CascadeType.REMOVE removes children when the parent is removed; CascadeType.ALL means all operations. Cascades act on operations we call on the parent, such as em.remove(order).

orphanRemoval is not a cascade type. It is a separate attribute of @OneToMany and @OneToOne that says a child which loses its parent is an orphan and must be deleted. It reacts to changes in the relationship itself, not just to operations on the parent.

2Seeing the Difference in Code

Take an Order with lines, mapped with mappedBy. We try two operations under each setting: removing the whole order, and removing one line from the collection.

Java
// Setting A: cascade = CascadeType.REMOVE only
em.remove(order); // delete 3 lines, then delete the order
order2.removeLine(line); // line.order = null, lines.remove(line)
// update order_line set order_id = null where id = ? (line survives as an orphan)
// or a constraint violation if order_id is NOT NULL
// Setting B: orphanRemoval = true (often with cascade = ALL)
em.remove(order); // delete 3 lines, then delete the order
order2.removeLine(line);
// delete from order_line where id = ? (orphan deleted)
What each setting deletes
ActionCascadeType.REMOVEorphanRemoval = true
em.remove(parent)children deletedchildren deleted
child removed from collectionnot deleted (FK nulled)deleted
child's parent reference clearednot deleteddeleted
child moved to another parentmoveddeleted, so avoid moving
orphanRemoval covers the parent removal case and adds deletion when a child is detached.

So for removing the parent the two behave the same. The difference is the second case. With only REMOVE, taking a line out of the collection just breaks the link, leaving a row with no order, or failing on a NOT NULL constraint. With orphanRemoval, the line is deleted because it no longer belongs to anything. In other words, orphanRemoval implies the removal behaviour and adds deletion on disassociation.

3How the Deletes Are Executed

Both settings work through the persistence context, Hibernate's set of managed entities. To remove an order's children, Hibernate first needs them as managed entities, so it loads the collection if it is not loaded yet, then schedules a delete for each child, runs any cascades further down (for example from lines to their discounts), calls lifecycle callbacks such as @PreRemove, and at flush sends one DELETE per row (batched if JDBC batching is enabled).

Java
em.remove(order); // order with 2,000 lines
// select ... from order_line where order_id = ? (load all 2,000)
// delete from order_line where id = ? x 2,000
// delete from orders where id = ?

For a few dozen lines this is fine and has real advantages: callbacks run, the second-level cache stays correct, and the persistence context knows the rows are gone. For thousands of rows it is slow and memory-hungry, which is when the database should take over.

4Only for Children the Parent Owns

Both settings say the parent controls the child's life. That is true for composition, where the child has no meaning without the parent: order lines, invoice items, the answers of a submitted survey. It is false for shared or independent entities. Putting REMOVE or orphanRemoval on Customer.orders would delete a customer's entire order history when the customer is removed, and on a @ManyToMany it would delete entities other parents still use. So the rule is simple: only for exclusively owned children, and never on @ManyToOne or @ManyToMany towards shared entities.

5Database ON DELETE CASCADE as the Alternative

A foreign key declared with ON DELETE CASCADE makes the database delete child rows when the parent row is deleted, in one statement and without sending rows to the application. In Hibernate we can declare it with @OnDelete(action = OnDeleteAction.CASCADE) so schema generation adds it, and Hibernate then knows it can skip deleting those children itself.

Java
@Entity class OrderLine {
@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
Order order;
}
// order_line.order_id REFERENCES orders(id) ON DELETE CASCADE
em.remove(order);
// delete from orders where id = ? (database removes the 2,000 lines itself)

The trade-off is that the deletes happen out of Hibernate's sight. @PreRemove callbacks and entity listeners on the children do not run, Envers does not record the child deletions, and any child entities already loaded in the current persistence context or the second-level cache are now stale. That is acceptable for large, simple child tables and a poor fit where children have callbacks or auditing. Check your Hibernate version for exactly where @OnDelete may be placed.

6Follow-up questions

Should we set both cascade = ALL and orphanRemoval = true?

For true composition, yes, it is the common combination: ALL so that persisting and merging the order handles its lines, and orphanRemoval so that removing a line from the collection deletes it. orphanRemoval alone does not cascade persist.

Can orphanRemoval delete a child we only wanted to move?

Yes. If we remove a line from order A and add it to order B, the removal marks it as an orphan and it may be deleted. Moving owned children between parents is a sign they are not exclusively owned, or that we should copy them instead.

Does a JPQL bulk delete of orders trigger either setting?

No. Bulk DELETE statements go straight to the database and bypass cascades and orphan handling. Children must be deleted first in their own statements, or the foreign key needs ON DELETE CASCADE.

7Conclusion

In this answer, we compared CascadeType.REMOVE and orphanRemoval. REMOVE deletes children when the parent is removed; orphanRemoval does that and also deletes a child removed from the collection or detached from its parent. Both load the children into the persistence context and delete them one by one, which runs callbacks but is slow for big trees, so both belong only on exclusively owned children. For large trees, ON DELETE CASCADE in the database, declared with @OnDelete, is faster but bypasses JPA callbacks and the persistence context.

What to say in the interviewAbout 80 seconds aloud

Open with

CascadeType.REMOVE means that when I remove the parent, the remove is cascaded to its children, so deleting an order deletes its lines. orphanRemoval covers that case too, but it adds one more: when a child is removed from the parent's collection, or its reference to the parent is cleared, it's treated as an orphan and deleted.

Then, if they want more

So with only REMOVE, order.removeLine(line) just nulls the foreign key, and the row survives or hits a not-null constraint, while with orphanRemoval that line is deleted. Both work through the persistence context: Hibernate loads the children, runs callbacks like @PreRemove, cascades further down, and sends one delete per row at flush.

That's fine for a few dozen lines but slow for thousands. Both also mean the parent owns the child's whole life, so I only use them for composition, like order lines or invoice items, never for shared entities like a customer's orders or across a many-to-many.

For large trees I'd consider ON DELETE CASCADE on the foreign key, which Hibernate can declare with @OnDelete. It's one statement and much faster, but the database does it behind Hibernate's back, so entity callbacks and Envers don't see the child deletes, and anything already loaded in the persistence context or second-level cache is stale.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between CascadeType.REMOVE and orphanRemoval = true on a...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/5e67ff7ae6/