Hi,
I have two related tables with a one to many relationship.
I am using NHibernate's subclass definition to create entities based on the value of the foreign key column in the "many" table. This I can get to work fine - example mapping follows:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="XXX.BusinessEntities" assembly="XXX.ApplicationServices">
<class polymorphism="explicit" name="ComboBoxEntry" table="ComboBoxes" discriminator-value="0">
<id name="Id" column="CB_ID" type="Int32">
<generator class="identity" />
</id>
<discriminator column="CB_CBN_ID" type="Int32" not-null="true"></discriminator>
<property name="Description" column="CB_Description" type="String" length="50" not-null="true" />
<subclass discriminator-value="3" name="Zone">
<many-to-one name="ComboBox" column="CB_CBN_ID" insert="false" update="false" not-null="true" />
</subclass>
<subclass discriminator-value="4" name="TrackType">
<many-to-one name="ComboBox" column="CB_CBN_ID" insert="false" update="false" not-null="true" />
</subclass>
<subclass discriminator-value="5" name="RaceType">
<many-to-one name="ComboBox" column="CB_CBN_ID" insert="false" update="false" not-null="true" />
</subclass>
</class>
</hibernate-mapping>
As you can see I create a Zone, TrackType and RaceType entity based on the CB_CBN_ID column.
What I dislike about this is the dependency on an actual value in the foreign key - what if the value is different between deployments (all primary keys are identity columns) ?
What I would like to do is refer to the code in the "one" table that relates to that discriminator value. For example, using ComboBox.Code - this removes the dependency on the identity column.
Is this possible? If so, can somone please post an example?
Thanks in advance,
Mike :)