-->
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.  [ 6 posts ] 
Author Message
 Post subject: Does Hibernate detect wrong inserts in a FK?
PostPosted: Wed Oct 05, 2005 4:02 am 
Newbie

Joined: Tue Jul 26, 2005 3:54 am
Posts: 16
Hi,

I'm new in hibernate and have a simple question. When I don't create any keys in a db itself, just in the mapping-files of hibernate I create ids(PK) and many-to-one relations to simulate foreign keys.
Will hibernate detect in the mapping if I would make wrong inserts to a FK-property?

Thanks in advance and sorry for my bad english,
Karl


Top
 Profile  
 
 Post subject: Re: Does Hibernate detect wrong inserts in a FK?
PostPosted: Wed Oct 05, 2005 8:16 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
KarlW wrote:
Hi,

I'm new in hibernate and have a simple question. When I don't create any keys in a db itself, just in the mapping-files of hibernate I create ids(PK) and many-to-one relations to simulate foreign keys.
Will hibernate detect in the mapping if I would make wrong inserts to a FK-property?

Thanks in advance and sorry for my bad english,
Karl


If the database doesn't enforce your FK relationships, Hibernate won't either.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Does Hibernate detect wrong inserts in a FK?
PostPosted: Wed Oct 05, 2005 8:18 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pksiv wrote:
KarlW wrote:
Hi,

I'm new in hibernate and have a simple question. When I don't create any keys in a db itself, just in the mapping-files of hibernate I create ids(PK) and many-to-one relations to simulate foreign keys.
Will hibernate detect in the mapping if I would make wrong inserts to a FK-property?

Thanks in advance and sorry for my bad english,
Karl


If the database doesn't enforce your FK relationships, Hibernate won't either.


Actually, that's not true. When reading an object, if hibernate can't find the <many-to-one> key in the associated table, it will throw an exception. You can define this behavior using the not-found="exception|ignore" attribute on the mapping.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 9:02 am 
Newbie

Joined: Tue Jul 26, 2005 3:54 am
Posts: 16
So If I get you right, Hibernate works like that:

It saves an object o to table x.
After that I want to save another object ao to table y.
In the hibernate mapping of table y exists a many-to-one-relation with the first table x.
Before saving object ao to table y, hibernate will look in the database in table x if there is an object o.

Is that right???

Regards, Karl


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 9:06 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
KarlW wrote:
So If I get you right, Hibernate works like that:

It saves an object o to table x.
After that I want to save another object ao to table y.
In the hibernate mapping of table y exists a many-to-one-relation with the first table x.
Before saving object ao to table y, hibernate will look in the database in table x if there is an object o.

Is that right???

Regards, Karl


No. Hibernate won't make sure that the o exists in table x when saving ao to table y without a FK constraint on the database. But it will throw an exception when reading the object if not-found="ignore" isn't specified on the <many-to-one>

You don't really want Hibernate to do all those extra database reads do you ? That's what database constraints are for.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:37 am 
Newbie

Joined: Tue Jul 26, 2005 3:54 am
Posts: 16
You are right of course, thats the job of the database. What a stupid question I asked... With large datasets to execute a select-query every time couldn't be performant.

Thanks for your help,
Karl


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