-->
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.  [ 8 posts ] 
Author Message
 Post subject: Multiple composite foreign keys sharing a column
PostPosted: Thu Dec 08, 2005 7:33 am 
Newbie

Joined: Mon Dec 05, 2005 10:02 am
Posts: 12
Hi All,

I have an address table which has (among others) the columns COUNTRY_ID, STATE_ID, AREA_CODE and ZIP_CODE.
The following foreign keys apply:
COUNTRY_ID from the countries table
COUNTRY_ID+STATE_ID from the states table
COUNTRY_ID+AREA_CODE from the area codes table
COUNTRY_ID+ZIP_CODE from the zip-codes table

I have mapped entities for countries, states, area codes and zip codes.
Now I managed to map the address class successfully only when setting all my many-to-one relations to update="false" insert="false".
The data is read perfectly, but I need to update those properties as well. Naturally, when setting them they are not updated in the db.

I understand why it is problematic to update a column used in many foreign keys but probably there is some kind of paradigm to overcome this problem elegantly.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 6:20 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Two questions:
1. Can you be more specific, why you can't map Country_ID column, for example?
2. Is it possible for you to change the DB structure? Composite keys are rarely necessary, and i suppose your case could live quite happily w/o them.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 12:01 pm 
Newbie

Joined: Mon Dec 05, 2005 10:02 am
Posts: 12
Hi xor,

Thanks for your reply. Per your questions:
1.
I can't use the same column COUNTRY_ID in many composite ID mappings because NHibernate throws a mapping exception when I'm trying to do so telling me that I must use update="false" insert="false" when reusing the same column. This is no good because I need to be able to update those fields.

2.
Actually I can change the database structure, but I am reluctant to do so. IMO using natural candidate keys has many advantages compared to synthetic keys.
If I don't have a choice I might change the database structure but it's a bit unesthetic, I think, to change a valid and working database schema because you persistence tool can't cope with it well.
Nonetheless, I like NHibernate very much, don't take offence :lol:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 12:43 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
Just so that everyone is clear, the Hibernate mantra has been that both surrogate keys and natural keys are supported, but surrogate keys are preferred. From the book "Hibernate in Action",
Quote:
Chapter 3, Section 3.4.3 "Choosing primary keys"

Experience has shown that natural keys almost always cause problems in the long run. ... we strongly recommend that new application use synthetic identifiers (also called surrogate keys).


So we have opposing preferences. Big deal? Shouldn't be. But the difference is there. In extreme cases it becomes a bit like putting a round peg in a square hole, but for most cases (n)Hibernate should be able to work with most schemas.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 5:47 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Oh, i got it, drorwe. Of course, if you have several many-to-one relationships sharing one column, then NH don't want to give you ability to save object, because it afraids that it will not be able to complete.

Reasons obvious, i think. In the memory many-to-one link to the country looks the same as a link to a state - it's just a reference to object. There are no restrictions for you to set a "country" field to country A, and "state" field to the state, belonging to the country B. In this case NH will not be able to save your object.

Please note that this is my interpretation which could be different from official ideas of Hibernate architects.

P.S. I will not argue about natural keys, but on my experience they are almost always inconvenient. In fact, they are "natural" only for human brain, not for DBMS or O/R mappers. :)

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 3:05 pm 
Newbie

Joined: Mon Dec 05, 2005 10:02 am
Posts: 12
xor & k-dub, thank you both for your replies.
I guess I will have to introduce surrogate keys where the natural-candidate key is composite.
Still, I plan to keep the natural keys in cases when the key is a single column. Anybody sees problem with that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 5:03 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
drorve, it depends, of course. :) But i'm sure you understand everything right.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:40 am 
Newbie

Joined: Thu Mar 02, 2006 9:18 am
Posts: 3
Hi drorwe,

I have exactly the same problem. Did you find a way to go around it?

Thanks
Guillaume


PS : As far as I'm concerned, I'm allowed to change databases models so... no way to use surrogate keys unstead of composite ones.


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