nHibernate 2.0-映射复合ID *和*多对一关系会导致“无效索引".错误 [英] nHibernate 2.0 - mapping a composite-id *and* many-to-one relationship causes "invalid index" error
问题描述
我有问题.想象一下这个数据模型:
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
对象中同时查看Person
和Tag
对象.因此,我在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
对象并保存它时,我得到一个此SqlParameterCollection的无效索引n,计数为n""错误.我知道这是因为我已经将PersonId
和TagId
属性映射了两次,一次是针对复合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屋!