public class Base {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<Order> orders;
}
public class Order {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<Product> products;
}
public class Product {
//fields
}
When trying to update Base with updated products ( removing product from one of the Order), I am getting A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance: com.xx.yy.zz.Order.products
Before saveAndFlush(base)
, I am clearing the data as well.
base.getOrders().forEach(order -> order.getProducts().clear());
base.getOrders().clear();
base.getOrders().addAll(baseToUpdate.getOrders());
How do I make sure that products are cleared before adding the new orders?