NHibernate 映射——一对一(或一对零) [英] NHibernate mapping - one-to-one (or one-to-zero)

查看:23
本文介绍了NHibernate 映射——一对一(或一对零)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NHibernatians!

NHibernatians!

我有一个表 [dbo].[Wibble] 和另一个表 [dbo].[WibbleExtended].

I have a table [dbo].[Wibble] and another table [dbo].[WibbleExtended].

[Wibble] 是主表,[WibbleExtended] 是可选表,其中存储了一些其他字段.[WibbleExtended] 表中的条目比主 [Wibble] 表中的条目少得多.我认为这是为了解决一些空间问题(Wibble 有很多行而 WibbleExtened 有很多列).

[Wibble] is the main table and [WibbleExtended] is an optional table where some other fields are stored. There are far fewer entries in the [WibbleExtended] table than the main [Wibble] table. I think this was done back in the day to cure some space issues (Wibble has many rows and WibbleExtened has many columns).

每个表的 ID 都是相同的,并且来自外部来源.

The ID for each table is the same and comes from an external source.

[dbo].[Wibble].[WibbleId]

[dbo].[WibbleExtended].[WibbleId]

是相同的,并且是两个表之间的关系.

are identical and is how the two tables relate.

注意我无法更改架构.我将其硬塞到我几乎无法控制的遗留系统上.

N.B. I can't change the schema. I'm shoe-horning this onto a legacy system that I have almost no control over.

环顾四周,似乎一对一映射有问题,普遍的智慧是使用两个多对一映射.

Searching around it seems that one-to-one mappings are problematic and the prevailing wisdom is to use two many-to-one mappings.

我目前的映射是:

<class name="Wibble" table="Wibble" >
   <id name="Id" column="WibbleId" type="Int32">
      <generator class="assigned"/>
   </id>

   <many-to-one name="WibbleExtended" class="WibbleExtended" column="WibbleId" not-null="false" cascade="all"/>
</class>

<class name="WibbleExtended" table="WibbleExtended" >
   <id name="Id" column="WibbleId" type="Int32">
        <generator class="assigned" />
   </id>

   <many-to-one name="Wibble" class="Wibble" column="WibbleId" not-null="true" />     
 </class>

问题是我收到了诸如

System.IndexOutOfRangeException: Invalid index n for this SqlParameterCollection with Count=n.

我环顾四周,这看起来确实是正确的策略,只是在最后的障碍上失败了.

I've looked around and this does look like the correct strategy, it's just falling at the final hurdle.

是id生成器的问题吗?映射的其他方面?

Is the problem the id generator? Other aspect of the mapping?

获得正确答案的免费肉馅饼.

Free mince pie for the correct answer.

好的 - 这是我通过@James Gregory 解决这个问题的方法.

Ok - here's what I did to solve this via @James Gregory.

  1. 将单元测试从 WibbleExtended 测试移到 Wibble 测试类并进行必要的修改.

  1. Moved the unit tests from the WibbleExtended tests to the Wibble test class and made the necessary modifications.

将以下内容添加到 Wibble.hbm.xml

Added the following to the Wibble.hbm.xml

<join table="WibbleExtended" optional="true">
     <key column="WibbleId"/>
     <property name="Blah1" column="Blah1" type="String" length="2000" not-null="false" />
     <property name="Blah2" column="Blah2" type="String" length="1000" not-null="false" />    
</join>

  • 为 Wibble POCO 添加了相应的属性.

  • Added the corresponding properties to the Wibble POCO.

    删除了所有与 WibbleExtended 相关的代码.

    Deleted all code relating to WibbleExtended.

    运行测试,全部通过,签入.构建通过.去喝圣诞啤酒(因此我更新这个已经过了几天!:-))

    Run tests, all passed, checked in. Build passed. Went for an xmas beer (hence it's been a couple of days before I updated this! :-))

    推荐答案

    您是否考虑过使用 加入 NHibernate 2.0 中引入的元素?此元素允许您连接多个表以形成一个实体;这种关系也可以是可选的.

    Have you considered using the Join element that was introduced in NHibernate 2.0? This element allows you to join multiple tables to form one entity; that relationship can also be optional.

    这篇关于NHibernate 映射——一对一(或一对零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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