๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-03-01 22:10:30.100 ERROR 8842 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.hibernate.HibernateException: A collection with cascade='all-delete-orphan' was no longer referenced by the owning entity instance] with root cause org.hibernate.HibernateException: A collection with cascade='all-delete-orphan' was no longer referenced by the owning entity instance

โšก Quick Fix Works 80% of the time

Do not assign a new ArrayList to the field. Use collection.clear() instead.

// BAD user.setOrders(new ArrayList<>()); // GOOD user.getOrders().clear();

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

You used orphanRemoval = true on a @OneToMany relationship. This tells Hibernate to delete child entities when they are removed from the list. However, you replaced the entire list object with a new instance (e.g., setOrders(new ArrayList<>())). Hibernate was still tracking the old list and lost the connection.

The HITEC City Parking Spot Analogy:

Imagine a dog catcher tracking stray dogs by putting collars on them. If someone takes the collars off and puts them on a completely new set of dogs, the catcher (Hibernate) is confused about where the original dogs went.

๐Ÿ” How to Reproduce Confirm this is your error

Reassign a brand-new ArrayList to a @OneToMany(cascade = ALL, orphanRemoval = true) collection instead of calling clear(), then save the entity. Hibernate throws 'collection with cascade all-delete-orphan was no longer referenced'.

๐Ÿ› ๏ธ Solutions (3 Ways to Fix)

Solution 1โœ“ Most common cause

Use clear() instead of new ArrayList()

๐Ÿ‘‰ Use this if you are trying to remove all child entities.

Hibernate tracks the specific List object it gave you. If you replace it with a brand new ArrayList, Hibernate loses track of the orphans and crashes. You must clear the existing list.

@Entity public class User { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List<Order> orders = new ArrayList<>(); } // Service Layer: user.getOrders().clear(); // Correct way to remove all // user.setOrders(new ArrayList<>()); // WRONG! Throws exception
Solution 2

Keep the field initialization in the Entity

๐Ÿ‘‰ Use this to prevent NullPointerExceptions when adding items.

Always initialize the collection to a new ArrayList at the field declaration level, never in the setter.

public class User { // Initialize here private List<Order> orders = new ArrayList<>(); // Do NOT do this in setter: // public void setOrders(List<Order> orders) { this.orders = new ArrayList<>(orders); } }
Solution 3

Remove orphanRemoval if not needed

๐Ÿ‘‰ Use this if you don't actually need to delete children when they are removed from the list.

If you just want to sever the relationship, but keep the child record in the DB, remove orphanRemoval = true.

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL) // removed orphanRemoval = true private List<Order> orders;

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Uses Hibernate 5. Strict about reference swapping.

Spring Boot 3.x

Uses Hibernate 6. Identical behavior.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Never replace collection fields in JPA entities. Always use add(), remove(), or clear() on the existing collection.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase