I have a @OneToMany collection (list) that I would like to clear, and add new elements to in the same transaction.
Using
collection.clear();
collection.add(new EntityB());
Simply adds the new instance, and never removes anything. I have orphanRemoval = true
for the collection field.
ADDED:
// Parent entity
@OneToMany(mappedBy = "product", orphanRemoval = true)
private List<Feature> features = new ArrayList<>();
// Child entity
@ManyToOne(cascade = CascadeType.ALL)
private Product product;
// Clear and add attempt
product.getFeatures().clear();
Feature feature = new Feature(product, ls);
product.getFeatures().add(feature);
Turns out that the actual solution was using a @JoinColumn annotation instead of the mappedBy="" parameter.