nHibernate 2.0 - 映射复合 ID * 和 * 多对一关系导致“无效索引"错误 [英] nHibernate 2.0 - mapping a composite-id *and* many-to-one relationship causes "invalid index" error

查看:15
本文介绍了nHibernate 2.0 - 映射复合 ID * 和 * 多对一关系导致“无效索引"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.想象一下这个数据模型:

I have a problem. Imagine this data model:

[Person] table has: PersonId, Name1  
[Tag] table has:  TagId, TagDescription  
[PersonTag] has: PersonId, TagId, IsActive

由于 [PersonTag] 不仅仅是一个简单的多对多连接表,我在 nHibernate 中创建了所有三个实体(就像它们在数据模型中一样).因此,PersonTag 需要一个复合 ID,我已将其映射到这样的类:

Since [PersonTag] isn't just a simple many-to-many join table, I have all three entities created in nHibernate (exactly like they are in the data model). PersonTag, therefore, needs a composite-id, which I have mapped to a class like this:

<composite-id name="PersonTagKey" class="PersonTagKey">
  <key-property name="PersonId"></key-property>
  <key-property name="TagId"></key-property>
</composite-id> 

我想遍历对象图,并能够从检索到的 PersonTag 对象中查看 PersonTag 对象.所以,我在 PersonTag 对象上有属性来做到这一点,映射如下:

I want to traverse the object graph and be able to look at both the Person and Tag objects from a retrieved PersonTag object. So, I have properties on the PersonTag object to do that, mapped like this:

<many-to-one name="Person" column="PersonId" lazy="proxy" cascade="none" class="Person"/>
<many-to-one name="Tag" column="TagId" lazy="proxy" cascade="none" class="Tag"/>

当我尝试创建一个 PersonTag 对象并保存它时,我收到一个 Invalid index n for this SqlParameterCollection with Count=n" 错误.我知道这是因为我已经将 PersonIdTagId 属性映射了两次,一次用于复合 ID,一次用于多对一关系.如果我不映射多对一对象,则一切正常.

When I try to create a PersonTag object and save it, I get an "Invalid index n for this SqlParameterCollection with Count=n" error. I know this is because I've mapped the PersonId and TagId properties twice, once for the composite-id, and once for the many-to-one relationship. If I don't map the many-to-one objects, then everything works fine.

有什么方法可以让我基于在同一个 nHibernate 实体中建模的同一个列建立一个复合 ID 和一个多对一关系?

推荐答案

凯,这就是答案.几乎没有关于此的文档:

Kay, here's the answer. Little-to-no documentation on this:

<composite-id name="PersonTagKey" class="PersonTagKey"> 
  <key-many-to-one name="Person" column="PersonId" lazy="proxy" class="Person">
  <key-many-to-one name="Tag" column="TagId" lazy="proxy" class="Tag"/>
</composite-id>

这将允许您创建一个由多对一关系的逆组成的复合 ID.

This will allow you to create a composite-id made up of the inverse of a many-to-one relationship.

良好的狩猎...

这篇关于nHibernate 2.0 - 映射复合 ID * 和 * 多对一关系导致“无效索引"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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