-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can you have referential integrity in Hibernate?
PostPosted: Thu May 19, 2011 9:50 am 
Newbie

Joined: Thu May 19, 2011 9:39 am
Posts: 2
Hi, I'm new to Hibernate and I have a problem which I have been googling it for hours. Basically, I have a one-to-many relationship between table A and table B. What I would like to do is to prevent a record from being inserted into table A unless its foreign key is actually referencing an existing record in table B. Does anyone know how to do this in Hibernate via JPA? Thanks


Top
 Profile  
 
 Post subject: Re: Can you have referential integrity in Hibernate?
PostPosted: Thu May 19, 2011 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes, easy: don't care about it as it will do that automatically.
Just make sure you define your model properly, then check the generated schema.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Can you have referential integrity in Hibernate?
PostPosted: Fri May 20, 2011 2:53 am 
Newbie

Joined: Thu May 19, 2011 9:39 am
Posts: 2
Can you give me some tips as to how to do it? I looked in the documentation but I could not find anything. Hibernate is not raising an exception when I insert a record which has a foreign key that references a non-existing record. Should I define the constraints at the database level?


Top
 Profile  
 
 Post subject: Re: Can you have referential integrity in Hibernate?
PostPosted: Fri May 20, 2011 4:07 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Hibernate is not raising an exception when I insert a record which has a foreign key that references a non-existing record.

that's obviously impossible, how could it store something violating the database constraints.
- did you disable database contraints (or run MySQL with MyISAM which doesn't support them)
- you're not mapping objects on the relations, but mapping primitives representing foreign keys

Quote:
Can you give me some tips as to how to do it? I looked in the documentation but I could not find anything.

because it's trivial and automatic, but still Hibernate docs take the assumption people know how to work with databases.
You should first understand how to map objects and relations, I suspect that's the issue.. could you show a very simple relation?

_________________
Sanne
http://in.relation.to/


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