在基类上使用property-ref的Nhibernate映射 [英] Nhibernate mapping with property-ref on base class

查看:182
本文介绍了在基类上使用property-ref的Nhibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在管理某种通用表的旧式数据库上使用NHibernate.我会在下面解释.
我有一个包含3个字段的基本"表(我删除了不相关的字段):

基础
(
    地址( int 主要 )
    ClassId( int )
    标识符(字符串)
)



在ClassId和Identifier上有一个唯一的约束. Identifier字段包含派生表的ident字段(带分隔符,并不总是相同)的串联.


然后,我有许多派生表与基本表共享地址(存在一对一关联),每个派生表都有其自己的自然标识符.
例如,

 Derived1
(
    地址( int 主要 )
    ID1(字符串)
    ID2(字符串)
)


Id1和Id2是唯一约束的一部分.

衍生2
(
    地址( int 主要 )
    ID1(字符串)
    Derived1Ident(字符串)
)


id1是唯一约束的一部分.
Derived1Ident是Derived1项上的指针",但它使用标识符的级联而不是地址"字段.

图像

如果我创建一个Id1 ="D1"和Id2 ="A"的Derived1项,然后创建一个Id1 ="D2"的Derived2项,指向先前的Derived1项,则将创建这样的行:

图像


我将以下映射用于NHibernate:

 <   class    名称  ="    判别器值  ="  0" <   id     ="  地址" 类型   int"   名称  =" 地址" <  发电机    ="   hilo" <   param     ="  表" <  > 
                <   param    名称  列" <  > 
                <   param    名称   max_lo" <  > 
            <  /generator  > 
        <  /id  > 
        <  区分符    ="   ClassId" 类型   int" / > 
        <  属性    ="  标识符" 类型  字符串" / > 
        <  子类    ="   Derived1" 判别器值   1 " <  加入    ="   Derived1" <      ="  地址" > 
                <  属性    ="   Id1" 唯一键   ident " > 
                <  属性    ="   Id2" 唯一键   ident " > 
                <  属性    ="   StringVal1" 类型  字符串" / > 
                <  属性    ="   IntVal1" 类型   int" / > 
                <  属性    ="   DateVal1" 类型   datetime" / > 
            <  /join  > 
        <  /subclass  > 
        <  子类    ="   Derived2" 判别器值   2 " <  加入    ="   Derived2" <      ="  地址" > 
                <  属性    ="   Id1" 唯一键   ident " > 
                <  属性    ="   StringVal1" 类型  字符串" / > 

                <!-  <多对一名称="Derived1Ident "class =" Test5Derived1"property-ref ="标识符"/>  <  /join  > 
        <  /subclass  > 
    <  /class  >  



在我添加多对一(在上面的映射xml中注释)之前,它工作良好,然后出现以下错误:

在参考表``Base''中没有与外键``FK91E675AD9AF7A29''中的参考列列表匹配的主键或候选键.
无法创建约束.请参阅先前的错误.





看来NHibernate并没有使用鉴别符来查找具有匹配标识符的行.
我尝试使用其他继承映射模型,但无法弄清楚如何使用区分符where子句指向基类标识符字段.
有没有办法用NHibernate映射这些遗留表?一个属性,并在ClassId和Identifier字段上添加唯一键

 <   class    名称  ="    判别器值  ="  0" <   id     ="  地址" 类型   int"   名称  =" 地址" <  发电机    ="   hilo" <   param     ="  表" <  > 
                <   param    名称  列" <  > 
                <   param    名称   max_lo" <  > 
            <  /generator  > 
        <  /id  > 
        <  区分符    ="   ClassId" 类型   int" / > 
        <  属性    ="   ClassId" 类型   int"    更新  ="  false" 插入  ="     ="   key1"      / <  属性    ="  标识符" 类型  字符串"    唯一键  ="  key1"   / > 
        <  子类    ="   Derived1" 判别器值   1 " <  加入    ="   Derived1" <      ="  地址" > 
                <  属性    ="   Id1" 唯一键   ident " > 
                <  属性    ="   Id2" 唯一键   ident " > 
                <  属性    ="   StringVal1" 类型  字符串" / > 
                <  属性    ="   IntVal1" 类型   int" / > 
                <  属性    ="   DateVal1" 类型   datetime" / > 
            <  /join  > 
        <  /subclass  > 
        <  子类    ="   Derived2" 判别器值   2 " <  加入    ="   Derived2" <      ="  地址" > 
                <  属性    ="   Id1" 唯一键   ident " > 
                <  属性    ="   StringVal1" 类型  字符串" / > 

                <  多对一   名称  ="     class   ="  Test5Derived1" 属性参考  标识符"  外键  ="  > 
            <  /join  > 
        <  /subclass  > 
<  /class  >  



我希望这可以帮助其他人;)


I am trying to use NHibernate on a legacy database that manages some sort of generic tables. I''ll explain below.
I have a "base" table with 3 fields (I removed unrelevant fields) :

Base
(
    Address (int, Primary key)
    ClassId (int)
    Identifier (string)
)



There is a unique constraint on ClassId and Identifier. The Identifier field contains the concatenation of the ident fields (with separators, not always the same) for the derived table.


Then I have many derived tables that share the Address with the base table (there is a one-to-one association) and each derived table has its own natural identifiers.
For example,

Derived1
(
    Address (int, Primary key)
    Id1 (string)
    Id2 (string)
)


Id1 and Id2 are part of a unique constraint.

Derived2
(
    Address (int, Primary key)
    Id1 (string)
    Derived1Ident (string)
)


Id1 is part of a unique constraint.
Derived1Ident is a "pointer" on a Derived1 item but it uses the concatenation of identifiers not the Address field.

image

If I create a Derived1 item with Id1 = "D1" and Id2 = "A", then a Derived2 item with Id1 = "D2" pointing on previous Derived1 item, this would create rows like this :

image


I use the following mapping for NHibernate :

<class name="Base" discriminator-value="0">
        <id column="Address" type="int" name="Address">
            <generator class="hilo">
                <param name="table">Addresses</param>
                <param name="column">next_value</param>
                <param name="max_lo">100</param>
            </generator>
        </id>
        <discriminator column="ClassId" type="int"/>
        <property name="Identifier" type="string"/>
        <subclass name="Derived1" discriminator-value="1">
            <join table="Derived1">
                <key column="Address"/>
                <property name="Id1" unique-key="ident"/>
                <property name="Id2" unique-key="ident"/>
                <property name="StringVal1" type="string"/>
                <property name="IntVal1" type="int"/>
                <property name="DateVal1" type="datetime"/>
            </join>
        </subclass>
        <subclass name="Derived2" discriminator-value="2">
            <join table="Derived2">
                <key column="Address"/>
                <property name="Id1" unique-key="ident"/>
                <property name="StringVal1" type="string"/>

                <!--<many-to-one name="Derived1Ident" class="Test5Derived1" property-ref="Identifier"/>-->
            </join>
        </subclass>
    </class>



It works well until I add the many-to-one (commented in the mapping xml above) then I get the following error :

There are no primary or candidate keys in the referenced table ''Base'' that match the referencing column list in the foreign key ''FK91E675AD9AF7A29''.
Could not create constraint. See previous errors.





It seems NHibernate doesn''t use the discriminator to find the row with the matching Identifier.
I tried with other inheritance mapping models but I can''t figure out how to point on the Base class Identifier field with a discriminator where clause.
Is there a way to map these legacy tables with NHibernate ?

解决方案

I eventually found a way to map this legacy model using foreign-key="none" on many-to-one property and adding a unique-key on ClassId and Identifier fields

<class name="Base" discriminator-value="0">
        <id column="Address" type="int" name="Address">
            <generator class="hilo">
                <param name="table">Addresses</param>
                <param name="column">next_value</param>
                <param name="max_lo">100</param>
            </generator>
        </id>
        <discriminator column="ClassId" type="int"/>
        <property name="ClassId" type="int" update="false" insert="false" unique-key="key1" />
        <property name="Identifier" type="string" unique-key="key1" />
        <subclass name="Derived1" discriminator-value="1">
            <join table="Derived1">
                <key column="Address"/>
                <property name="Id1" unique-key="ident"/>
                <property name="Id2" unique-key="ident"/>
                <property name="StringVal1" type="string"/>
                <property name="IntVal1" type="int"/>
                <property name="DateVal1" type="datetime"/>
            </join>
        </subclass>
        <subclass name="Derived2" discriminator-value="2">
            <join table="Derived2">
                <key column="Address"/>
                <property name="Id1" unique-key="ident"/>
                <property name="StringVal1" type="string"/>

                <many-to-one name="Derived1Ident" class="Test5Derived1" property-ref="Identifier" foreign-key="none"/>
            </join>
        </subclass>
</class>



I hope this can help someone else ;)


这篇关于在基类上使用property-ref的Nhibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆