Quick revision
The short answer
Hibernate detects orphans by watching the collection instance it loaded or wrapped, its own PersistentBag or PersistentSet. order.setLines(new ArrayList<>(newLines)) throws that instance away, so Hibernate can no longer tell which old lines were removed and refuses at flush with this error. The fix is to modify the existing collection: order.getLines().clear(); order.getLines().addAll(newLines);, setting each new child's order reference so the owning side is correct, and ideally removing the public collection setter so it cannot happen again.
What to remember
In this answer, we saw that Hibernate replaces our collections with its own tracked instances, which hold the snapshot used to find orphans. Reassigning the field with setLines(new ArrayList<>(...)) leaves that instance unreferenced, so Hibernate cannot decide what to delete and throws. The fix is to modify the existing collection with clear() and addAll() through a domain method that also sets each child's order reference, and to remove public collection setters so the mistake cannot recur.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Hibernate detects orphans by watching the collection instance it loaded or wrapped, its own PersistentBag or PersistentSet. order.setLines(new ArrayList<>(newLines)) throws that instance away, so Hibernate can no longer tell which old lines were removed and refuses at flush with this error. The fix is to modify the existing collection: order.getLines().clear(); order.getLines().addAll(newLines);, setting each new child's order reference so the owning side is correct, and ideally removing the public collection setter so it cannot happen again.
Why this question is asked
This exception usually appears in update code, for example when a REST endpoint receives the full list of order lines and the service tries to replace the old list with the new one. The code reads perfectly well, so the message, with its odd mention of all-delete-orphan, can be confusing. Interviewers ask about it because it reveals whether we understand that Hibernate's collections are not plain Java lists. In this answer, we will recap how Hibernate tracks collections, reproduce the error, explain why replacing the instance breaks orphan detection, fix it, and see how to design entities so the mistake is impossible.
1A Quick Recap: Hibernate's Own Collections
When Hibernate loads an Order, it does not put a java.util.ArrayList into the lines field. It puts in its own implementation, a PersistentBag for a List (or PersistentSet for a Set). This wrapper remembers which session it belongs to, whether it has been loaded, and a snapshot of its elements as they were when loaded. When we persist a new Order, Hibernate also replaces our ArrayList with such a wrapper.
That snapshot is how orphanRemoval works. At flush, Hibernate compares the collection's current elements with its snapshot. Anything in the snapshot but no longer in the collection has been removed, and with orphanRemoval it is deleted. The name all-delete-orphan in the message is Hibernate's internal name for cascade ALL combined with orphan removal.
2Reproducing the Error
Here is the mapping and the update code from the question.
6 messages in order. Play them, or step through them one at a time.
The snapshot lives in the original collection instance, so the instance must stay.
The PersistentBag with its snapshot of the three old lines is no longer referenced by the order. Hibernate still knows about it, because it registered it when loading, but the entity now points somewhere else. Should the three old lines be deleted? Were they moved into the new list? Hibernate cannot answer safely, so it stops instead of guessing, which is better than silently deleting or silently keeping data.
3Fix: Change the Existing Collection
We keep Hibernate's collection and change its contents. Clearing it marks every old line as removed, and adding the new ones registers them for insert. Because the collection is the mapped-by side, we must also set each new line's order reference, which is what Hibernate uses to write the foreign key.
Notice that the collection field is now never reassigned after construction. The method expresses the business operation, replace the lines, and keeps both sides of the association correct. Deleting all lines and inserting new ones is fine for small collections; if lines have identity that matters, such as audit history, a smarter merge that updates matching lines by a key and only adds or removes the differences is better, and it uses the same in-place approach.
4Designing Entities So It Cannot Happen
The root cause is a public setter for a managed collection, often generated by an IDE or Lombok's @Setter or @Data. Entities with orphanRemoval collections should have no collection setter at all. They expose domain methods such as addLine, removeLine and replaceLines, and a getter that returns an unmodifiable view. Mapping frameworks such as MapStruct or ModelMapper that copy a DTO into an entity often call collection setters too, so configure them to use the domain methods or map collections by hand.
The same error can also come from code that assigns null to the collection, or from Jackson deserializing a request body directly into a managed entity. Both are avoided by the same rule: request DTOs are mapped into entities through domain methods, never by replacing fields.
5Follow-up questions
Why does this only happen with orphanRemoval?
Without orphan removal, Hibernate has nothing to decide about the old elements: removing an element from an inverse collection does not delete anything, so a replaced collection is simply treated as the new state. With orphan removal, the old elements might need deleting, and only the original instance's snapshot can tell.
Is it safe to call clear() and then addAll() with some of the same lines?
Yes, if they are the same managed instances: Hibernate sees them removed and re-added in the same flush and does not delete them. With new instances that merely have the same ids, it is safer to update the existing lines in place.
What if the new lines come from a detached Order passed to merge()?
merge() copies state onto the managed entity, and Hibernate handles collections by updating the managed collection's contents, so it works. The error appears when our own code reassigns the field on a managed entity.
6Conclusion
In this answer, we saw that Hibernate replaces our collections with its own tracked instances, which hold the snapshot used to find orphans. Reassigning the field with setLines(new ArrayList<>(...)) leaves that instance unreferenced, so Hibernate cannot decide what to delete and throws. The fix is to modify the existing collection with clear() and addAll() through a domain method that also sets each child's order reference, and to remove public collection setters so the mistake cannot recur.
Open with
Hibernate doesn't keep my ArrayList; when it loads or persists an Order, it puts its own collection, a PersistentBag, into the lines field. That object holds a snapshot of the elements, and that's how orphan removal works: at flush it compares the current contents to the snapshot and deletes whatever was removed.
Then, if they want more
When I call setLines with a brand new ArrayList, the order no longer references Hibernate's collection, so Hibernate can't tell whether the old lines were removed or moved, and rather than guess it throws this exception. all-delete-orphan is just its name for cascade ALL plus orphanRemoval.
The fix is to modify the existing collection instead of replacing it: getLines().clear(), then addAll the new lines. Since the collection is the mappedBy side, I also have to set each new line's order to this order, otherwise the foreign key is written as null.
In practice I'd put that in a replaceLines method on the entity that clears the list and calls addLine for each new line.
And I'd remove the public setter for the collection entirely, including any Lombok @Setter or @Data on the entity, and return an unmodifiable view from the getter, so neither our code nor a mapping library can reassign it again.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "This code throws 'A collection with cascade=all-delete-orphan was no longer referenced...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/931a4681f7/