多对多连接表鉴别器?

时间:2010-06-17 16:40:56

标签: nhibernate mapping many-to-many

我正在尝试通过连接表“Communication_Recipients”将我创建的具有2个属性“SuccessRecipientList”和“FailRecipientList”的Communication对象链接到Users对象。我想在连接表上使用鉴别器而不是为它创建一个实际的域对象(使用连接表上的HasFailed Bit列)有没有人知道是否可以这样做?

通信HBM:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataLogic" namespace="DataLogic.Domain">
  <class name="DataLogic.Domain.Communication, DataLogic" table="Communications" >
    <id name="Id" column="Id" type="Int32" unsaved-value="0">
      <generator class="identity"></generator>
    </id>
    ...
    <set name="SuccessRecipientList" table="Communication_Recipients" lazy="true">
      <key column="Communication_ID"></key>
      <many-to-many class="MilkroundOnline.OnlineApplications.DataLogic.Domain.User, MilkroundOnline.OnlineApplications.DataLogic" column="User_ID"></many-to-many>
    </set>
    <set name="FailedRecipientList" table="Communication_Recipients" lazy="true" where="" >
      <key column="Communication_ID"></key>      
      <many-to-many class="MilkroundOnline.OnlineApplications.DataLogic.Domain.User, MilkroundOnline.OnlineApplications.DataLogic" column="User_ID"></many-to-many>
    </set>
  </class>
</hibernate-mapping>

DB看起来像:

通讯表

ID,
主题,
身体

用户表

ID,
名字,
姓氏

CommunicationUser Table

CommunicationId,
用户ID,
HasFailed(位)

提前感谢您的帮助!
罗布

1 个答案:

答案 0 :(得分:2)

不,多对多表只能映射键(如果是列表或字典,则加上索引或映射键,如果是idbag,则为自己的id)。

您需要创建一个实体。但是,从对象模型中可以投影两个集:

//mapped set
public virtual ICollection<CommunicationUser> RecipientList { get; set; }

public virtual IEnumerable<User> SuccessRecipientList
{
    get { return from cu in RecipientList where !cu.HasFailed select cu.User; }
}

public virtual IEnumerable<User> FailedRecipientList
{
    get { return from cu in RecipientList where cu.HasFailed select cu.User; }
}