-->
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: Cascade delete - Hibernate is TOO good
PostPosted: Sat Feb 12, 2005 8:14 am 
Newbie

Joined: Fri Dec 24, 2004 3:16 am
Posts: 5
Hi,

I am using Hibernate 3.0b4

I have an author and a book class with a many-to-many association between them.
In the author mapping file my 'books'-set has cascade=none. In the book mapping file my 'authors'-set has cascade=delete. With these settings I thought I would achieve the following:
*) when I delete a book, the corresponding relation with author will be deleted automatically by hibernate
*) when I delete an author, the delete should fail if the author is still associated with any books. So hibernate does not delete the relation with book.

The problem is that when I delete an author using session.delete(author) the corresponding entries from the table 'BOOK_AUTHOR' are deleted as well. So the cascade=none setting doest not seem to have any effect.

I am still pretty new to hibernate and I am used to trust on the relational integrity rules from by database to make sure my application will not delete any data that should not be deleted. So Hibernate actually does too good a job for me, because it weakens my referential integrity rules.

Is there a way to prevent hibernate to cascade-delete at one side of a many-to-many relation and to allow the cascade-delete on the other side of the relation and if so, how could I achieve this? Am I doing something wrong?

Thanks a lot.

Luc Feys


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 12, 2005 8:43 am 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
Cascade aplies to other side of association.
So the behaviour in your situation wil be ass falows:
When you delete Author, association will be deleted too and Book will not be deleted.
When you delete Book, association and Autor will be deleted.
If you want to have cascade only for many-to-many association you can create additional association class and use two one-to many associations instead of one many-to-many.
Try to use not-null in this association class on reference to Autor.
Now if you try to delete Autor, constraint exception will occur.

_________________
Lmichasz


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.