While working on group resource implementation, I came across a fascinating hibernate issue.
ca.uhn.fhir.rest.server.exceptions.InternalErrorException: Failed to call access method:
org.springframework.orm.hibernate4.HibernateSystemException: A collection with cascade="all-
delete-orphan" was no longer referenced by the owning entity instance:
org.openmrs.Cohort.memberships; nested exception is org.hibernate.HibernateException: A
collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
org.openmrs.Cohort.memberships
Hibernate gives enough info (stack trace) about the error to identify what’s going on. Hibernate is complaining of not being able to track the change for the child collection object cohortMembership
while it is getting set in the parent object cohort
. Hibernate requires that parent object owns a child collection object completely.
To fix this, we need to modify the following code;
Old code: openmrs-core/Cohort.java at master · openmrs/openmrs-core · GitHub
public void setCohortMemberships(Collection<CohortMembership> members) {
this.memberships = members;
}
New code
public void setCohortMemberships(Collection<CohortMembership> members) {
this.memberships.addAll(members);
}
It should fix the issue, though I haven’t tried yet. So this requires modifying openmrs core
. For now, I don’t want to do that. So is there any workaround or easy fix for this that does not require me modifying openmrs core?