NHibernate-如何使用父子引用映射Composite-id [英] NHibernate - How to map composite-id with parent child reference

查看:133
本文介绍了NHibernate-如何使用父子引用映射Composite-id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况(为了更好地说明,该示例非常简化) 数据库模型 BAS_COSTCODE指的是BAS_CONTEXT.该表的键是具有COSTCODEID和CONTEXT_FK的组合. CONTEXT_FK是指BAS_CONTEXT. 要构建分层树,CostCode可以有一个父级.因此,对表本身有一个引用.

i have the following scenario (For better illustration, the example is very simplified) Database Model BAS_COSTCODE refers to BAS_CONTEXT. The key for this table is a composite with COSTCODEID and the CONTEXT_FK. CONTEXT_FK refers to BAS_CONTEXT. To build up a hierarchical tree, a CostCode can have a parent. For this reason there is a reference to the table itselfe.

上下文模式文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="App.NHibernate.DataObjects" namespace="App.NHibernate.DataObjects">
      <class name="App.NHibernate.DataObjects.Context" table="BAS_CONTEXT" lazy="false" mutable="true" >
        <id name="Id" type="Int16" column="CONTEXTID" >
          <generator class="assigned"/>
        </id>
        <property name="Name" column="NAME" type="String" not-null="true"/>
       </class>
    </hibernate-mapping>

CostCode的模式如下:

And the schema for CostCode is like that:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="App.NHibernate.DataObjects" namespace="App.NHibernate.DataObjects">
  <class name="App.NHibernate.DataObjects.CostCode" table="CON_COSTCODE" mutable="true" >

    <composite-id name="CompositeId" class="CostCodeId" unsaved-value="any">
      <key-property name="Id" column="COSTCODEID" type="String" />
      <key-many-to-one name="Context" column="CONTEXT_FK" class="Context" lazy="proxy"/>
    </composite-id>

    <many-to-one name="ParentCostCode" class="CostCode" lazy="proxy" insert="true" update="true" >
      <column name="PARENTCOSTCODE_FK"/>
      <column name="CONTEXT_FK"/>
    </many-to-one>

    <property name="Name" column="NAME" type="String"/>
  </class>
</hibernate-mapping>

如果我创建一个新的CostCode实体并运行Commit(),则会收到以下异常: System.IndexOutOfRangeException:此Count = 13的SqlParameterCollection的索引13无效."

If i create a new CostCode entity and run Commit() i get the following exception: System.IndexOutOfRangeException: "Invalid index 13 for this SqlParameterCollection with Count=13."

我认为NHibernate的参考ParentCostCode出现了问题,该参考指的是父CostCode对象. NHIbernate假定将值写入PARENTCOSTCODE_FK列和CONTEXT_FK.在映射中,composite-id指向CONTEXT_FK,而ParentCostCode指向CONTEXT_FK.因此,它们共享"同一列,而NHibernate仅应将值写入PARENTCOSTCODE_FK列.

I think NHibernate has a problem with the reference ParentCostCode, which refers to a parent CostCode object. NHIbernate assumes to write a value to column PARENTCOSTCODE_FK and to CONTEXT_FK. In the mapping the composite-id points to CONTEXT_FK and the ParentCostCode points to CONTEXT_FK. So they are "sharing" the same column and NHibernate should only write a value to the column PARENTCOSTCODE_FK.

一种解决方案是在表BAS_COSTCODE中添加一个额外的列(可能是PARENTCONTEXT_FK)以表示父对象的Context. 但是我不想再增加一列,因为如果存在对父对象的引用,那么CONTEXT_FK和PARENTCONTEXT_FK的值必须具有相同的值.那将是多余的.

One solution is to add an additional column (maybe PARENTCONTEXT_FK) to table BAS_COSTCODE to represent the Context for the parent object. But i don't want to have an additional column, because the values of CONTEXT_FK and PARENTCONTEXT_FK must have the same value, if there exist a referenceto a parent object. And that would be redundant.

是否存在针对此类问题的更好解决方案?还是我无法阻止BAS_COSTCODE中的其他列?

Are there better solutions for that kind of problem? Or i can't prevent a additional column in BAS_COSTCODE?

在此先感谢您的答复!

推荐答案

直接使用非复合键.

<id>
  <generator class="hilo">
     <!-- ... -->
  </generator>
</id>

<property name="CostCode" column="COSTCODEID" unique-key="CostCodeContext"/>

<many-to-one name="Context" >
  <column name="CONTEXT_FK" unique-key="CostCodeContext"> 
</many-to-one> 

<many-to-one name="Parent" class="CostCode">
  <column name="PARENT_FK"/>
</many-to-one>

这篇关于NHibernate-如何使用父子引用映射Composite-id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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