nhibernate多对多映射 - 映射表中的附加列?

Gre*_*reg 11 nhibernate nhibernate-mapping

我有以下映射定义:

  <class name="Role" table="Role" optimistic-lock="version" >

    <id name="Id" type="Int32" unsaved-value="0" >
      <generator class="native" />
    </id>

    <property name="RoleName" type="String(40)" not-null="true" />

    <bag name="UsersInRole" generic="true" lazy="true" cascade="all" table="UserRoles" >
      <key  column="RoleId" />
      <many-to-many column="UserId" class="SystemUser, Domain"/>
    </bag>
Run Code Online (Sandbox Code Playgroud)

<id name="Id" type="Int32" unsaved-value="0" >
  <generator class="native" />
</id>
<property name="UserName" type="String(40)" not-null="true" unique="true" />
Run Code Online (Sandbox Code Playgroud)

此映射生成映射表UserRoles,它有两列 - RoleId和UserId.

但是,我想为该关系添加额外的属性 - 即一些枚举值定义关系的状态以及有效的开始和结束日期.

是否可以在nhibernate中执行或者我是否需要在此处添加其他类并将关系m-to-m更改为2个关系[user] 1-to-m [user_role] m-to-1 [role]?

Ben*_*tBe 9

您需要在代码中添加一个额外的类,例如UserRole,以保留其他属性.

当涉及到映射时,可以将其映射为您提到的类.但我也认为它可以映射为角色映射中的复合元素:

<set name="UsersInRole" lazy="true" table="UserRoles" >
  <key  column="RoleId" />
  <composite-element class="UserRole">
     <many-to-one name="User" column="UserId" not-null="true"/>
     <propery name="RelationState" not-null="true"/>
     <propery name="StartDate" not-null="true"/>
     <propery name="EndDate" not-null="true"/>
  </composite-element>
</set>
Run Code Online (Sandbox Code Playgroud)

所有属性必须不为null,因为它们已成为UserRoles表的主键的一部分.有关更多信息,请参阅