I faced a similar problem recently. For me, the issue was that the orphans were still referenced from another managed entity, and there was a PERSIST cascading defined for that relationship:
// Parent entity
@OneToMany(mappedBy = "product", orphanRemoval = true)
private List<Feature> features = new ArrayList<>();
// Child entity
@ManyToOne
private Product product;
@ManyToOne
private Description description;
// Another entity (let's say descriptions can be shared between features)
@OneToMany(mappedBy = "description", cascade = CascadeType.PERSIST)
private List<Feature> features = new ArrayList<>();
Assume that all of the involved entities are managed, i.e. loaded into the persistence context. Now we do the same thing as the OP:
// Clear and add attempt
product.getFeatures().clear();
Feature feature = new Feature(product, ls);
product.getFeatures().add(feature);
Philosophically, the issue here is that the object model becomes inconsistent if you only remove the Feature from the Product entity, but not from the Description entity. After all, you want the Feature to be removed, but it is still being referenced from other objects. Technically, what happens is that there are two conflicting cascadings going to the Feature entity, and the result may depend on the order in which they are applied.
Since the Feature was removed from the collection in product, the orphan removal applies, and the Feature entity transitions to "removed" state during the next flush, as stated in the JPA 2.1 spec (2.9). I added emphasis on the relevant parts:
Associations that are specified as OneToOne or OneToMany support use
of the orphanRemoval option. The following behaviors apply when
orphanRemoval is in effect:
- If an entity that is the target of the
relationship is removed from the relationship (by setting the
relationship to null or removing the entity from the relationship
collection), the remove operation will be applied to the entity being
orphaned. The remove operation is applied at the time of the flush
operation. The orphanRemoval functionality is intended for entities
that are privately “owned” by their parent entity. Portable
applications must otherwise not depend upon a specific order of
removal, and must not reassign an entity that has been orphaned to
another relationship or otherwise attempt to persist it. If the entity
being orphaned is a detached, new, or removed entity, the semantics of
orphanRemoval do not apply.
However, the same Feature is still referenced from a Description entity, which has a PERSIST cascading towards the Feature. The JPA 2.1 spec says the following:
The semantics of the flush operation, applied to an entity X are as
follows:
So this cascading will perform a "persist" operation on the Feature entity, even if we don't call em.persist() on the Description. It is enough for the Description to be managed when a flush is performed to trigger this persist cascading.
This means we are doing exactly what the spec told us we shouldn't - performing orphan removal and persisting on the same entity. What seems to happen in practice in Hibernate is that both operations are applied in turn. First the remove operation makes the Feature entity transition to "removed" state, then the persist operation turns the removed entity back into a managed one. As a result, the Feature is not removed from the database.
feature.setProduct(null)
simply clearing the collection (as you did), but with (at least) theCascadeType.PERSIST
in theProduct
entity and that works without problems.