C# NHibernate 映射——一对一(或一对零)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/388914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:35:04  来源:igfitidea点击:

NHibernate mapping - one-to-one (or one-to-zero)

c#nhibernatenhibernate-mapping

提问by Iain Holder

NHibernatians!

荷兰人!

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

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

[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).

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

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

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

I.e.

IE

[dbo].[Wibble].[WibbleId]

and

[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.

四处搜索似乎一对一映射是有问题的,普遍的智慧是使用两个多对一映射。

My mappings currently are:

我的映射目前是:

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

And

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

The problem with this is I'm getting errors such as

问题是我收到了错误,例如

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.

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

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

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

Free mince pie for the correct answer.

正确答案的免费肉馅饼。

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

编辑:好的 - 这是我通过@James Gregory 解决这个问题的方法。

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

  2. 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>
    
  3. Added the corresponding properties to the Wibble POCO.

  4. Deleted all code relating to WibbleExtended.

  5. 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! :-))

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

  2. 将以下内容添加到 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>
    
  3. 为 Wibble POCO 添加了相应的属性。

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

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

采纳答案by James Gregory

Have you considered using the Join elementthat 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 2.0 中引入的Join 元素?此元素允许您连接多个表以形成一个实体;这种关系也可以是可选的。

回答by AlexCuse

Check out this link: http://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html

查看此链接:http: //www.hibernate.org/hib_docs/nhibernate/html/inheritance.html

The "joined subclass" mapping in particular I think you will find helpful (assuming WibbleExtended inherits from Wibble).

特别是“加入的子类”映射,我认为您会发现很有帮助(假设 WibbleExtended 继承自 Wibble)。

回答by Jalal El-Shaer

The error you are getting:

你得到的错误:

Invalid index n for this SqlParameterCollection with Count=n.

此 SqlParameterCollection 的索引 n 无效,Count=n。

is due to two properties mapped to the same column. Use insert=false and update=false in one of the two.

是由于两个属性映射到同一列。在两者之一中使用 insert=false 和 update=false 。

reference http://groups.google.com/group/nhusers/browse_thread/thread/84830b1257efd219

参考http://groups.google.com/group/nhusers/browse_thread/thread/84830b1257efd219