views:

14

answers:

1

Hi all,

I'm trying to link a Communication object I've created that has 2 properties "SuccessRecipientList" and "FailRecipientList" to a Users object, via joining table "Communication_Recipients". I'd like to do this using a discriminator on the joining table instead of creating an actual domain object for it (Using the HasFailed Bit column on the join table) Does anyone know if this can be done?

Communication 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>

The DB looks like:

Communication Table

ID,
Subject,
Body

User Table

ID,
Firstname,
Lastname

CommunicationUser Table

CommunicationId,
UserId,
HasFailed(Bit)

Thanks in advance for any help!
Rob

A: 

No, a many-to-many table can only map the keys (plus index or map key if it's a list or dictionary, and its own id if it's an idbag).

You'll need to create an entity. However, from your object model you can project both sets:

//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; }
}
Diego Mijelshon
Thats great! I hadn't thought about it like that. Thanks so much for your answer!
Rob Taylor

related questions