|
Hi guys,
I have a class modelled as a parent-child relationship (using one-to-many) and marked as cascade="all-delete-orphan". Because of the database structure, the primary key of each child is a composite identifier consisting of the parent id and the line number. The parent and child classes all extend a PersistentObject class that correctly identifies to a hibernate Interceptor whether that instance is persistent or transient.
1) An instance of the parent class with its children is loaded in one session, returned to the view, and the session closed.
2) Within the view, the parent is 're-rated', which involves removing all the children, discarding unwanted instances, and re-adding the wanted children to the parent. Some new children may also be created as a result of this process. Because the child's id consists of the parent id and the line no, this process can cause child objects to now have a different id.
3) The parent is then passed back to the view, where it needs (in a new hibernate Session) to be updated, and any children saved, updated or deleted as appropriate.
It is within step 3 that I am having problems. I have tried a number of different strategies including:
a) Calling update() on the parent (This causes an sql exception because it is trying to INSERT the child data into existing rows)
b) Calling saveOrUpdateCopy() on the parent (Hibernate throws an exception upon encountering the new (ie. transient) children)
c) Explicitly calling delete() on all the deleted children, then calling saveOrUpdate() on the parent (Hibernate throws an exception, saying that 'cascade would cause deleted instance to be re-saved')
d) as for c) above but also explicitly evicting each child after deleting it (This causes an sql exception when the sesison tries to INSERT child data into existing rows)
I had hoped to make use of the feature that clearing a persistent Hibernate collection (ie. the children) would cause the session (upon re-attaching the parent via update()) to automatically delete all persistent child instances. Then automatically save all child instances via the parent cascade. But it doesn't seem to be working as planned. The session doesn't seem to automatically delete the child elements.
Does anyone have any advice on the correct order of commands to achieve this? Should I explicitly delete the child elements, then flush and evict? And is there a correct way to use the cascade facility in this scenario?
Many thanks for any help on this.
Scott
|