0

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

Kyle Novak
  • 425
  • 6
  • 15

1 Answers1

0

I believe your problem is due to the fact that you have given your subclass its own primary key in the table, i.e. AccountAttribute_id in AccountAttribute.

As you said that you are using table-per-subclass all of your subclass tables should use the superclass primary key, therefore the AccountAttribute table should only have Attribute_id as a primary key which is also a foreign key back to the Attribute table.

Once you've made these changes your mapping should then work because it is using the correct <key />


Refs:

ChrisAnnODell
  • 1,341
  • 15
  • 24
  • I agree there should be only one primary key - unfortunately there are several things working against me here. #1 There is a field in the AccountAttribute table that will be updateable - thus the primary key is required. #2 The relationship between CustomerProfile & AccountAttribute is required as this is a way of dynamically adding attributes (i.e. city, state, zip, etc.) to the CustomerProfile without having to change the database schema. The more I think about it, the more I think inheritance is not the right way to approach this mapping. – Kyle Novak Jan 20 '11 at 14:58