[英]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
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]
不仅仅是一个简单的多对多连接表,我在nHibernate中创建了所有三个实体(就像它们在数据模型中一样)。 PersonTag
, therefore, needs a composite-id, which I have mapped to a class like this: 因此,
PersonTag
需要一个复合id,我已经映射到这样的类:
<composite-id name="PersonTagKey" class="PersonTagKey">
<key-property name="PersonId"></key-property>
<key-property name="TagId"></key-property>
</composite-id>
I want to traverse the object graph and be able to look at both the Person
and Tag
objects from a retrieved PersonTag
object. 我想遍历对象图,并能够从检索到的
PersonTag
对象中查看Person
和Tag
对象。 So, I have properties on the PersonTag
object to do that, mapped like this: 所以,我在
PersonTag
对象上有属性来做,像这样映射:
<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"/>
When I try to create a PersonTag
object and save it, I get an "Invalid index n for this SqlParameterCollection with Count=n" error. 当我尝试创建一个
PersonTag
对象并保存它时,我得到一个“此SqlParameterCollection的无效索引n,其中Count = n”错误。 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. 我知道这是因为我已经两次映射了
PersonId
和TagId
属性,一次是复合id,一次是多对一关系。 If I don't map the many-to-one objects, then everything works fine. 如果我不映射多对一对象,那么一切正常。
Is there some way for me to be able to have a composite-id AND a many-to-one relationship based on the same column modeled in the same nHibernate entity? 有没有办法让我能够在同一个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>
This will allow you to create a composite-id made up of the inverse of a many-to-one relationship. 这将允许您创建由多对一关系的倒数组成的复合ID。
Good hunting... 好狩猎......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.