-->
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: Repeated column in mapping for collection with composite-ids
PostPosted: Thu Dec 08, 2005 4:59 am 
Newbie

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

I'm having a relational model with composite IDS to map zip codes to area codes in countries, allowing the flexibility to have more than one area code per zip.

The database model is:

Table T_COUNTRY
COUNTRY_ID (PK)
COUNTRY_NAME

Table T_STATE
COUNTRY_ID (PK)
STATE_ID (PK)
STATE_NAME
(COUNTRY_ID is fk from T_COUNTRY)

Table T_AREA_CODE
COUNTRY_ID (PK)
AREA_CODE (PK)
STATE_ID
(COUNTRY_ID + STATE_ID are fk from T_STATE)

Table T_ZIP_CODE
COUNTRY_ID (PK)
ZIP_CODE (PK)
ZIP_LATITUDE
ZIP_LONGITUDE
(COUNTRY_ID is fk from T_COUNTRY)

Table T_ZIP_AREA_CODE
COUNTRY_ID (PK)
ZIP_CODE (PK)
ZIP_SEQ (PK)
AREA_CODE
(COUNTRY_ID+ZIP_CODE is fk from T_ZIP)
(COUNTRY_ID+AREA_CODE is fk from T_AREA_CODE)

I managed to map everything ok except the many to many relationship between ZIP and area code. I am only interested in the relationship from the ZIP side, so the area code mapping is OK.

Here's the mapping for the ZIP code class:

Code:
   <class name="NHibTest1.MEZip, NHibTest1" table="T_ZIP_CODE">
      <composite-id>
         <key-many-to-one name="Country" column="COUNTRY_ID" access="nosetter.camelcase-underscore"/>
         <key-property name="Code" column="ZIP_CODE" access="nosetter.camelcase-underscore" />
      </composite-id>      
      <property name="Latitude" column="ZIP_LATITUDE" access="nosetter.camelcase-underscore" />
      <property name="Longitude" column="ZIP_LONGITUDE" access="nosetter.camelcase-underscore" />
      <list name="Cities" table="T_ZIP_CITY" access="nosetter.camelcase-underscore" lazy="false" inverse="false">
         <key>
            <column name="COUNTRY_ID" />
            <column name="ZIP_CODE" />
         </key>
         <index column="ZIP_SEQ" />
         <element column="ZIP_CITY" type="string" />
      </list>
      <list name="AreaCodes" table="T_ZIP_AREA_CODE" access="nosetter.camelcase-underscore" lazy="false" inverse="false">
         <key>
            <column name="COUNTRY_ID" />
            <column name="ZIP_CODE" />
         </key>
         <index column="ZIP_SEQ" />
         <many-to-many class="NHibTest1.MEAreaCode, NHibTest1">
            <column name="COUNTRY_ID" />
            <column name="AREA_CODE" />
         </many-to-many>
      </list>
   </class>


When trying to run this I get the "Repeated column in mapping for collection" because COUNTRY_ID is used both as part of the key to access the T_ZIP_AREA_CODE table and as part of the key of an AREA_CODE from T_AREA_CODE. But logically this is ok because it is indeed the same country the one for the zip code and the one for the area code.
A possible workaround for this, I believe, will be adding additional COUNTRY_ID column to the db table and using one for access to the link table and one to define the area code key, but this is awkward.

Anybody know how to get this working right with those composite-ids?
I have also defined a class for composite key for area code - can it be used here in some way?

Any help will be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 8:31 pm 
Newbie

Joined: Wed Jan 11, 2006 1:04 pm
Posts: 5
Please let me know if you were able to solve the issue. I am having the same issue.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 11:38 am 
Newbie

Joined: Mon Dec 05, 2005 10:02 am
Posts: 12
I couldn't get the complete the mappings properly using composite keys. Luckily, I had the possibility of modifying the database schema and introducing surrogate (or synthetic) keys for the entities which have composite natural candidate solves the problem immediately so you don't have to combat those definitions.
So, for example, I had to introduce a new synthetic unique key column for table T_STATE instead of using COUNTRY_ID and STATE_ID. Once you do this many mapping chalenges are solved.
Although I prefer the database schema with the composite keys, making this change helped me complete the mapping for my application quite easily. Yhey argue that natural candidate keys are easy for humans to understand, but synthetic keys are easier for the computers.

Good luck with your application
Drpr


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 10:28 am 
Newbie

Joined: Wed Jan 11, 2006 1:04 pm
Posts: 5
Thank you very much for your response. We are also working on changing the database tables.


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.