I have an entity with an attribute that is a list of children. On its set, I clear the current list and set its new values from database, following the correct guideline for the "all-delete-orphan" problem with hibernate:
@JsonView(Views.Response.class)
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "uuid")
private Set<Response> responses = new HashSet<>();
public void setResponses(final Set<Response> responses) {
this.responses.clear();
if(responses != null) {
this.responses.addAll(responses);
}
}
By reading some Stack Overflow and other links on this error, I found that this might happen when you assign a new list to responses directly through =
. However, if you clear the list first and simply add new values, this should not happen. At some point I even found an article saying I should perform an entityManager.flush()
after the clear()
. For all I can see and have read, I am following the rules. What am I missing here? Why do I still get the following exception with the code above? I don't know if this information is useful but I am using Java 17.
javax.persistence.PersistenceException: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.ppro.transaction.model.Processing.acquirerResponses