aspose file tools*
The moose likes Object Relational Mapping and the fly likes do I have to specify FK if I don't update the other table ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "do I have to specify FK if I don Watch "do I have to specify FK if I don New topic
Author

do I have to specify FK if I don't update the other table ?

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
I asked this simple question a while ago but got no answer ---

I have table A which contains column c, column c is a foreign key (FK) referring to Table B. I only need to operate on table A. In this case, do I have to specify the foreign key relationship (1-to-1, 1-to-many, etc) in the hibernate mapping xml file ??

Thanks.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
I don't think that Hibernate would complain if you don't map this relationship. The database will enforce referential integrity, and Hibernate will just transmit to the application any exception messages from the database.
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Originally posted by Edvins Reisons:
I don't think that Hibernate would complain if you don't map this relationship. The database will enforce referential integrity, and Hibernate will just transmit to the application any exception messages from the database.


Thanks. Let me make sure I understand well --- So you mean I do not need to specify the relationship in hbm.xml file AND the POJO, is that right ? Actually I only have a FK referencing relation but there is not any cascade constraint between the two tables. So, is it OK for me to completely ignore the relationship when I create the hbm.xml config and POJO classes (2 POJO) ?
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
As I understand it, there is no obligation to map relationships for Hibernate. For the foreign key, the database is going to enforce that the values that you set in the foreign key column are present in the referenced column. ORM, including Hibernate, contains mappings that can be used to deal with this, but it's up to you to do otherwise, for example, check this in your application logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do I have to specify FK if I don't update the other table ?