NHibernate三元关系映射

时间:2011-02-10 19:28:14

标签: nhibernate nhibernate-mapping

我有以下表格

CREATE TABLE track
ID
name

CREATE TABLE people
ID
firstname

CREATE TABLE role
ID
name

CREATE TABLE track_people_role
track_id
people_id
role_id

以及以下映射文件

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="App.Data.BusinessObjects" assembly="App.Data">
<class name="App.Data.BusinessObjects.Track, App.Data" table="track" lazy="true">
    <id name="Id" column="ID">
        <generator class="native" />
    </id>
    <property name="Name" column="name" />
</class>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="App.Data.BusinessObjects" assembly="App.Data">
<class name="App.Data.BusinessObjects.People, App.Data" table="people" lazy="true">
    <id name="Id" column="ID">
        <generator class="native" />
    </id>
    <property name="Firstname" column="firstname" />

</class>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="App.Data.BusinessObjects" assembly="App.Data">
<class name="App.Data.BusinessObjects.Role, App.Data" table="role" lazy="true">
    <id name="Id" column="ID">
        <generator class="native" />
    </id>
    <property name="Name" column="name" />
</class>

我很难尝试为track_people_role表配置映射。请问有人帮帮我吗?

我在中间表中有下面的行。主键是三列的组合。

track_id  people_id  role_id  
14292     1          1  
14292     1          2  


track_id  people_id  role_id  
1200      1         1  
1200      2         1  

1 个答案:

答案 0 :(得分:1)

省略所有细微之处,这是一个有效的例子。

类别:

class Track
{
    ...
    public virtual IDictionary<People, Role> PeopleRoles { get; set; }
}

映射:

<class name="Track" ...>
  ...
  <map name="PeopleRoles" table="track_people_role">
    <key column="track_id"/>
    <map-key-many-to-many column="people_id" class="People"/>
    <many-to-many column="role_id" class="Role"/>
  </map>
</class>

虽然很简单,但它包含许多假设......请不要犹豫。