-->
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.  [ 2 posts ] 
Author Message
 Post subject: Bidirectional One-2-Many; cascade="none" cause sav
PostPosted: Thu Jan 13, 2005 9:49 am 
Newbie

Joined: Tue Jan 11, 2005 10:49 am
Posts: 3
Hibernate version: 2.1.3 & 2.1.6
Please help me about this confusing cascade of Hibernate:
I thought in any case when I set the cascade=none(on parent or one side in an association), the delete action of parent must cause an exception to be thrown, because when I set cascade=none, Hibernate supposed to don't react in a save-update or delete manner, but here it reacts in save-update manner.
I want to have a bidirectional one-2-many association, and when I want to delete the parent (which has at least one child), the hibernate throws an exception about foreign key constraint violation. I don't want hibernate delete the parent side and update all the children's foreign key to null (what I got as the result). So what should I do?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 4:34 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Lots of posts and information on this one. You need to mark the collection as inverse="true" which tells Hibernate that you will manage the relationship from the child side of things, otherwise Hibernate thinks the relationship is managed from the parent side. So, what happens is when you delete the parent Hibernate removes the relationship first by updating the child object to set its relationship field to null. Here's the sequence so you can understand it (Hibernate gurus, please correct me if I'm wrong...)

Inverse="false", cascade="none"
1. User deletes parent
2. Hibernate senses the relationships are being deleted
3. Hibernate updates child objects' relationship field to NULL, effectively deleting the relationship (this may result in a Foreign Key violation, if the column is not nullable in the DB)
4. Hibernate deletes the parent, orphaning the children

Inverse="true", cascade="none"
1. User attempts to delete parent
2. Hibernate senses the relationships are attempting to be deleted
3. Hibernate throws an exception

Please experiment with that and see what happens. Also, read and re-read the documentation and web site articles on inverse="true" relationships. Understanding that is crucial to understanding how hibernate works with relationships.


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

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.