-->
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: Why Hibernate doesn't check foreign keys at insert on MySql?
PostPosted: Tue Nov 18, 2003 10:41 am 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
I am using hibernate whith MySQL

I have an User object containing an Address object as reference (FK)
I am trying to insert an User object into the table.
For my point of view, Hibernate should check to see if Address object from User exists in the table address and if negative an specific Exception must be thrown.
If not so, I must do an extra find for Address before inserting in order to preserve the data integrity.

Other thing , about Unique Keys checks: Supose I have 2 unique keys in User object: userName and emailAddress.
When inserting a record which contains a duplicate value for one of those keys, how should I know wich of the fields are in fact duplicated only by looking at thrown Exception and not doing an extra find before insert?

Which is the best practice for those 2 cases?

Many thanks,

Florin Marcus.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 10:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The best practice is to use a database that implements foreign key constraints. For example, MySQL with innodb tables.

There is no way in the world that Hibernate should issue an SQL select to "check" that every associated object exists. Thats insane. A system like this could not possibly perform well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 10:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Your first assumption is half true, and it works like you would expect it. But because you haven't shown any real problem (exception, mappings, code), we can't help you here.

Your second question: How would you do it without Hibernate?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 11:54 am 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
Wow! That was fast! Finally a forum which really works!

Many thanks for clearing this up.

Best Regards, Florin Marcus


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.