in Requirement model class
@ElementCollection
@OneToMany(fetch = FetchType.EAGER, mappedBy = "requirementId", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<RequirementTestcaseMapping> testcaseVersionIds = new HashSet<>();
earlier we were using
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
`<hibernate.version>5.3.20.Final</hibernate.version>`
then we were not getting below error
now that we use
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>${hibernate.version}</version>
</dependency>
`<hibernate.version>5.6.15.Final</hibernate.version>`
we get below error
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.thed.model.Requirement.testcaseVersionIds
at org.hibernate.engine.internal.Collections.processDereferencedCollection(Collections.java:100) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.engine.internal.Collections.processUnreachableCollection(Collections.java:51) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.lambda$flushCollections$1(AbstractFlushingEventListener.java:251) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.engine.internal.StatefulPersistenceContext.forEachCollectionEntry(StatefulPersistenceContext.java:1140) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:248) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1407) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:489) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3303) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2438) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:449) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) ~[hibernate-core-jakarta-5.6.15.Final.jar:5.6.15.Final]
at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:645) ~[spring-orm-6.0.0.jar:6.0.0]
the issue gets fixed if we replace
public void setTestcaseVersionIds(Set<RequirementTestcaseMapping> testcaseVersionIds) {
if(this.testcaseVersionIds==null){
this.testcaseVersionIds = new HashSet<>();
}
if (testcaseVersionIds == null) {
this.testcaseVersionIds = testcaseVersionIds;
}else if (testcaseVersionIds.isEmpty()){
this.testcaseVersionIds = testcaseVersionIds;
}else{
this.testcaseVersionIds.clear();
this.testcaseVersionIds.addAll(testcaseVersionIds);
}
}
with
public void setTestcaseVersionIds(Set<RequirementTestcaseMapping> testcaseVersionIds) {
if (this.testcaseVersionIds == null) {
this.testcaseVersionIds = new HashSet<>();
}
this.testcaseVersionIds.clear();
if (testcaseVersionIds != null && !testcaseVersionIds.isEmpty()) {
this.testcaseVersionIds.addAll(testcaseVersionIds);
}
}
Why were we not getting the error with hibernate.version = 5.3.20.Final and below code? and why
public void setTestcaseVersionIds(Set<RequirementTestcaseMapping> testcaseVersionIds) {
if(this.testcaseVersionIds==null){
this.testcaseVersionIds = new HashSet<>();
}
if (testcaseVersionIds == null) {
this.testcaseVersionIds = testcaseVersionIds;
}else if (testcaseVersionIds.isEmpty()){
this.testcaseVersionIds = testcaseVersionIds;
}else{
this.testcaseVersionIds.clear();
this.testcaseVersionIds.addAll(testcaseVersionIds);
}
}