We’re having a bit of trouble with Collections, specifically one of them (out of 3 in this class, hundreds in total including another class that has a collection of the same type of elements).
What advice I’ve found says that great care must be taken not to replace the collection itself, only its elements. It therefore looks like this:
private List<TypeConstraint> typeConstraints = new ArrayList<>();
/* ... */
public void setTypeConstraints(List<TypeConstraint> typeConstraints) {
this.typeConstraints.clear();
this.typeConstraints.addAll(typeConstraints);
}
and is mapped as follows
<list cascade="all,delete-orphan" lazy="true" name="typeConstraints">
<key column="CHARGE_ID"/>
<list-index column="CHARGE_TYPE_POS"/>
<one-to-many class="se.zaleth.jar.generic.TypeConstraint" not-found="ignore"/>
</list>
As I said above, it is this collection in this class that generates the exception, not the identical collection in another class. Have we accidentially used a reserved keyword somewhere?
Edit: This is the exception
javax.persistence.PersistenceException: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: se.zaleth.jar.charge.Charge.typeConstraints
at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:120)
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: se.zaleth.jar.charge.Charge.typeConstraints
at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:120)
A test case can be found at this link: orphan-collection.zip - Google Drive