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.