I'm using the table per subclass mapping inheritance in NHibernate. I have a parent Attribute
table and a child AccountAttribute
table. The child AccountAttribute
table has another foreign key in it to a CustomerProfile
table. The CustomerProfile
table has a zero or more relationship to the AccountAttribute
table; meaning I'll have a collection of AccountAttributes
in my CustomerProfile
class.
How do I map the CustomerProfile
table to the AccountAttribute
table in my NHibernate mapping so that the CustomerProfile
class is hydrated with it's correct AccountAttributes
?
Tables
Attribute: Attribute_Id (PK)
AccountAttribute: AccountAttribute_Id (PK); Attribute_Id (FK); CustomerProfile_Id (FK)
CustomerProfile: CustomerProfile_Id (PK)
Mapping for the Attribute/AccountAttribute hierarchy.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="Attribute, CustomerProfile" lazy="false">
<id name="Identifier" column="Attribute_Id" access="field.camelcase">
<generator class="identity"/>
</id>
<joined-subclass name="AccountAttribute, CustomerProfile" table="AccountAttribute" lazy="false">
<key column="Attribute_Id" />
<property name="ValueText" column="Value_Txt" access="nosetter.camelcase" />
</joined-subclass>
</class>
</hibernate-mapping>
Mapping for the Account object
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="Account, CustomerProfile" lazy="false">
<id name="Identifier" column="Account_Id" access="field.camelcase">
<generator class="identity"/>
</id>
<!-- How do I map to the AccountAttributes table here to get the correct set? -->
<bag name="AccountAttributes" access="field.camelcase" table="AccountAttribute">
<key column="Account_Id" />
<one-to-many class="AccountAttribute, CustomerProfile"/>
</bag>
</class>
</hibernate-mapping>
Thanks,
Kyle