2011-01-19 80 views
0

我在NHibernate中使用每個子類映射繼承的表。我有一個父母Attribute表和一個孩子AccountAttribute表。 AccountAttribute表中的另一個外鍵包含在CustomerProfile表中。 CustomerProfile表與表AccountAttribute有零或多個關係;這意味着我將在我的CustomerProfile課程中收集AccountAttributes在NHibernate中加入到每個類層次結構的子類

如何映射CustomerProfile表到AccountAttribute表在我的NHibernate的映射,使得CustomerProfile類水合與它的正確AccountAttributes

屬性: Attribute_Id(PK)

AccountAttribute: AccountAttribute_Id(PK); Attribute_Id(FK); CustomerProfile_Id(FK)

CustomerProfile: CustomerProfile_Id(PK)

映射的屬性/ AccountAttribute層次。

<?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> 

映射爲Account對象

<?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> 

感謝,

凱爾

回答

0

我相信你的問題是由於這樣的事實,你給你的子類自己的主鍵該表,即AccountAttribute_idAccountAttribute

正如你說你正在使用表每個子類所有子類表應該使用超類主鍵,因此AccountAttribute表應該只有Attribute_id作爲主鍵,這也是一個外鍵回Attribute表。

一旦你做了這些改變你的映射應該再工作,因爲它使用的是正確<key />


參考文獻:

+0

我同意應該只有一個主鍵 - 不幸的是有幾件事情在這裏對我不利。 #1 AccountAttribute表中有一個可更新的字段 - 因此主鍵是必需的。 #2 CustomerProfile和AccountAttribute之間的關係是必需的,因爲這是一種動態添加屬性的方式(即,城市,州,郵編等)到CustomerProfile,而無需更改數據庫模式。我考慮得越多,我認爲繼承越多,就不是處理這種映射的正確方法。 – 2011-01-20 14:58:28