These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem updating detached parent-child relationship
PostPosted: Sun Nov 14, 2004 10:48 pm 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.