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

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

问题描述

NHibernatians!



我有一个表[DBO]。[维布勒]和另一个表[DBO]。[WibbleExtended]。



[维布勒]是主表和[WibbleExtended]是一个可选的表,其中的一些其他领域的存储。有在[WibbleExtended]表比主[维布勒]表少得多的条目。我觉得这样做是在当天返回治愈一些空间问题(维布勒有许多行和WibbleExtened有很多列)。



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



  [DBO]。[维布勒]。[WibbleId ] 

  [DBO]。[WibbleExtended]。[WibbleId] 

是相同的,就是如何这两个表关联。



NB我无法改变的模式。我里上演这到遗留系统,我有过几乎没有控制权。



围绕它在搜索似乎一对一的映射是有问题的,当时的。智慧是使用两个多到一的映射



我的映射当前是:

 <类名=维布勒表=维布勒> 
< ID名称=ID列=WibbleIdTYPE =的Int32>
<生成器类=分配/>
< / ID>

<多到一个名称=WibbleExtended级=WibbleExtended栏=WibbleId不空=假级联=所有/>
< /班>

 <类名=WibbleExtended表=WibbleExtended> 
< ID名称=ID列=WibbleIdTYPE =的Int32>
<生成器类=分配/>
< / ID>

将;多对一个名称=维布勒类=维布勒列=WibbleId非空=真/>
< /班>

的问题,这是我收到的错误,比如

  System.IndexOutOfRangeException:无效的指数n此SqlParameterCollection以计数= N。 



我环顾四周,这看起来像正确的战略,它只是下跌的最后一关



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



免费碎肉馅饼正确答案



编辑:?好吧 - 这里是我也通过@詹姆斯格雷戈里来解决这个问题。




  1. 移动从WibbleExtended测试单元测试的维布勒测试类,并提出必要的修改。


  2. 增加了以下的Wibble.hbm.xml

     <连接表=WibbleExtended可选=真> 
    <键列=WibbleId/>
    <属性名=Blah1栏=Blah1类型=字符串长度=2000不空=假/>
    <属性名=Blah2栏=Blah2类型=字符串长度=1000不空=假/>
    < /加入>


  3. 新增的相应属性的维布勒POCO。


  4. 删除有关WibbleExtended所有代码。


  5. 运行测试,全部通过,在检查。建立过去了。又一个圣诞节啤酒(因此它已经有几天我更新了这个前: - - !))



解决方案

你有没有使用考虑加入该元素在NHibernate的2.0引入?这个元素允许你加入多个表,形成一个实体;这种关系也可以是可选的。


NHibernatians!

I have a table [dbo].[Wibble] and another table [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).

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

I.e.

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

Free mince pie for the correct answer.

EDIT: Ok - here's what I did to solve this via @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! :-))

解决方案

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天全站免登陆