Hello,
I have the following problem. I need to use all-delete-orphan for the collection in the parent class, just like this:
Code:
<list name="branches" cascade="all-delete-orphan" inverse="false">
<key column="SECTION_ID" />
<index column="BRANCHES_INDEX" />
<one-to-many class="Branch" />
</list>
And I want to delete one parent moving all its children to another parent (recipient), just like this:
Code:
public boolean deleteAndMoveBranchesTo(Long victimId, Long recipientId) {
Section victim = (Section) getSession().load(Section.class, victimId);
Section recipient = (Section) getSession().load(Section.class, recipientId);
for (Branch branch : victim.getBranches()) {
branch.setSection(recipient);
getSession().saveOrUpdate(branch);
}
recipient.getBranches().addAll(victim.getBranches());
victim.getBranches().clear();
saveOrUpdate(recipient);
getSession().delete(victim);
return true;
}
But this code cause following error:
Code:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [org.jtalks.poulpe.model.entity.Branch#1]
at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1042)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:188)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:320)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:266)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:243)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)
...
So how can I combine all-delete-orphan, deletion of parent Section and moving all its children (Branches) to another Section?
It looks like this topic is related with
https://forum.hibernate.org/viewtopic.php?p=2350533Best regards, Dmitriy.