Loading a single entity from a many-to-many relationship using a flag

11 views
Skip to first unread message

Sean Irwin

unread,
Aug 11, 2011, 12:40:41 PM8/11/11
to nhu...@googlegroups.com

I have a many-to-many relationship between a Site (workplace) and an Individual with an intersection table SiteIndividual. The SiteIndividual table contains a flag,  IndividualWorkingSiteInd , that when true indicates the individuals primary workplace.

 

public class Site

{

    public virtual long SiteId { get; set; }

    public virtual string SiteName { get; set; }

    public virtual ICollection<SiteIndividual> SiteIndividuals { get; set; }

}

 

public class Individual

{

public virtual long IndividualId { get; set; }

public virtual string Name { get; set; }

public virtual ICollection<SiteIndividual> SiteIndividuals { get; set; }

}

 

public class SiteIndividual

{

  public virtual Site Site { get; set; }

  public virtual Individual Individual { get; set;}

  public virtual bool IndividualWorkingSiteInd { get; set; }

}

 

<class name="SiteIndividual" table="SITE_INDIVIDUAL" lazy="true" >

    <composite-id>

        <key-many-to-one name="Site" column="SITE_ID" lazy="proxy" class="Model.Site, Model"/>

        <key-many-to-one name="Individual" column="INDIVIDUAL_ID" lazy="proxy"

            class="Model.Individual, Model" />

    </composite-id>

    <property name="IndividualWorkingSiteInd" type="YesNo">

        <column name="INDIVIDUAL_WORK_SITE_IND" />

    </property>

</class>

 

Is there any way to load the Individuals primary site into a property, say PrimarySite of type Site, on the Individual entity or do I have to run multiple queries, one to load an Individual with all and then one to retrieve the Individuals primary work site?

 

public class Individual

{

    ....

    public virtual Site PrimarySite { get; set; }

}
 
I cannot change the database but is there a better way to implement my classes or my mapping files?
Reply all
Reply to author
Forward
0 new messages