NHibernate Map多对多联接表 [英] NHibernate Map many-to-many join table

查看:86
本文介绍了NHibernate Map多对多联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库结构如下:

Person
  Id
  Name
  FieldA
  FieldB

Phone
  Id
  Number

PersonPhone
  PhoneId
  PersonId
  IsDefault

我的Person和Phone对象的NHibernate映射很简单,这是我遇到的PersonPhone.我想将PersonPhone对象的集合作为Person的属性,这将使我能够获得一个人的电话号码,并能够分辨出哪个是该人的默认"电话或主要电话号码.

My NHibernate mappings for the Person and Phone objects are straight forward, its the PersonPhone I'm having difficult with. I want to have a collection of PersonPhone objects as a property of Person which will allow me to have the Phone number of a person and be able to tell which is the "default" or primary phone number for a person.

理想的是让我的PersonPhone对象看起来像这样:

ideally Id like my PersonPhone object to look like this:

public class PersonPhone
{
    public virtual Person Person { get; set; }
    public virtual Phone Phone { get; set; }
    public virtual bool IsDefault { get; set; }
}

到目前为止,我对该表的NHibernate映射如下所示:

so far my NHibernate mapping for this table looks like the following:

<class name="PersonPhone" table="PersonPhone">
    <composite-id>
        <key-property name="Person" column="PersonId" />
        <key-property name="Phone" column="PhoneId" />
    </composite-id>
    <property name="IsDefault" column="IsDefault"/>
</class>

但是当NHibernate编译我的映射时,我收到一条错误消息:

but when NHibernate compiles my mappings I get an error saying:

无法编译映射文档:MyApp.Entities.PersonPhone.hbm.xml. NHibernate.MappingException:无法确定以下类型:MyApp.Entities.Person,MyApp.Entities,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,用于以下列:NHibernate.Mapping.Column(PersonId)

Could not compile the mapping document: MyApp.Entities.PersonPhone.hbm.xml. NHibernate.MappingException : Could not determine type for: MyApp.Entities.Person, MyApp.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(PersonId)

关于如何映射的任何想法?

Any ideas on how this should be mapped?

推荐答案

答案是在复合键中使用元素,而不是键属性

The answer is to use the element in your composite key rather than the key-property

<class name="PersonPhone" table="PersonPhone">
    <composite-id>
        <key-many-to-one name="Person" column="PersonId"></key-many-to-one>
        <key-many-to-one name="Phone" column="PhoneId"></key-many-to-one>
    </composite-id>
    <property name="IsDefault" column="IsDefault"/>
</class>

这篇关于NHibernate Map多对多联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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