-->
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.  [ 7 posts ] 
Author Message
 Post subject: Defining foreign key constraints with Hibernate
PostPosted: Tue Oct 09, 2007 1:36 pm 
Newbie

Joined: Tue Oct 09, 2007 11:54 am
Posts: 5
Is there a way to define a foreign key constraint (forbid to delete rows that are foreign keys in other rows) with Hibernate?

@ForeignKey only seems to overwrite key names for relationships.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 3:25 am 
Newbie

Joined: Tue Oct 09, 2007 11:54 am
Posts: 5
No responses - then I take that as a "no, it's not possible". Astonishing...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 6:36 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Hi,
if you create a One-to-One, one-to-many or many-to-one relationship you are creating a foreign key with just the effect you describe!?

With the "cascade" attribute you can finetune it's behaviour.

So I don't really understand what you are missing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 6:56 am 
Newbie

Joined: Tue Oct 09, 2007 11:54 am
Posts: 5
Thanks, so how would you use "cascade" to model the foreign key restriction as outlined above?

I know that e.g. cascade = CascadeType.ALL would result in updates or deletes of the related table but how to define that a delete of a row is NOT possible if its id is still a foreign key of another row?

If a PARENT has CHILDS I want to prevent the deletion of a PARENT as long as there are CHILDS that belong to that PARENT.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 7:07 am 
Newbie

Joined: Tue Oct 09, 2007 11:54 am
Posts: 5
To make that clearer: I want that it is not even possible to delete these rows working outside of Hibernate, I want a real database constraint.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 2:49 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
If you don't specify any specific cascade you will get such a constraint at least with a Oralce10 Database (that's what we use).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 3:43 am 
Newbie

Joined: Tue Oct 09, 2007 11:54 am
Posts: 5
Good hint, does not work with MySQL (at least MYISAM, maybe INNODB does). That's why I thought I might have missed something.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.