๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-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)
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 exceptionKeep 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); }
}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
Uses Hibernate 5. Strict about reference swapping.
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.