2011-01-19 11 views
0

NHibernateでのサブクラスマッピング継承のテーブルを使用しています。私は親テーブルAttributeと子テーブルAccountAttributeを持っています。子AccountAttributeテーブルにはCustomerProfileテーブルに別の外部キーがあります。 CustomerProfileテーブルには、AccountAttributeテーブルとの関係が0以上あります。私のCustomerProfileクラスにAccountAttributesのコレクションがあることを意味します。 CustomerProfileクラスは、それはAccountAttributes正しいだと水和されるようにNHibernateのクラスごとの階層テーブルのサブクラスへの結合

は、どのように私はNHibernateのマッピングにAccountAttributeテーブルにCustomerProfileテーブルをマッピングしていますか?

属性: ATTRIBUTE_ID(PK)

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

CustomerProfile: CustomerProfile_Id(PK)属性/ AccountAttribute階層の

マッピング。 Accountオブジェクトのための

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

マッピング

<?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つだけである必要があることに同意します。残念ながら、ここで私に対抗するいくつかのことがあります。 #1 AccountAttributeテーブルには更新可能なフィールドがあるため、主キーが必要です。 #2これは、動的に属性を追加する方法であるため、CustomerProfileとAccountAttributeの関係は必須です(つまり、市、州、郵便番号など)をCustomerProfileに追加することができます。私はそれについてもっと考えるほど、私は継承がこのマッピングにアプローチする正しい方法ではないと思います。 –

関連する問題