views:

55

answers:

1

Hi all,
How would I go about mapping the following in NHibernate?
My entities and ERD are below. I know how to map a many-many relationship, but dont know how to map the joining table ReportTargets to the Datapoint table. You will notice that there is no ReportTargets entity model as it is not strictly a domain entity. What is the best solution here? I am a NHibernate newbie so go easy please..:) Thanks

+1  A: 

As MarketReport.Targets has a join table, map it as many-to-many.

<class name="MarketReport">
  <id column="reportid" />
  <bag name="ReportTargets" table="reporttargets">
    <key column="marketreportid"/>
    <many-to-many column="targetid" class="Target"/>
  </bag>
</class>

<class name="Target">
  <id column="targetid" />
  <bag name="DataPoints" inverse="true">
    <key column="targetid"/>
    <one-to-many class="DataPoint"/>
  </bag>
</class>

<class name="DataPoint">
  <id column="datapointid" />
</class>

Based on your most recent comment, you want either want a ternary association, or a component collection. I have included both mappings.

<class name="MarketReport">
    <id column="reportid" />
    <map name="ReportTargets" table="reporttargets">
        <key column="marketreportid"/>
        <index-many-to-many column="targetid" class="Target"/>
        <many-to-many column="datapointid" class="DataPoint"/>
    </map>
</class>

<class name="MarketReport">
    <id column="reportid" />
    <bag name="ReportTargets" table="reporttargets">
        <key column="marketreportid"/>
        <composite-element class="ReportTarget">
            <many-to-one name="Target" column="targetid"/>
            <many-to-one name="DataPoint" column="datapointid"/>
        </composite-element>
    </bag>
</class>

<class name="Target">
    <id column="targetid" />
</class>

<class name="DataPoint">
    <id column="datapointid" />
</class>
Lachlan Roche
Hi Lachlan, thanks for the continued help. Your recent edit sets up a direct one to many between Target and DataPoint, how would you create the link through the same join table, ie. ReportTargets, that is used for the many-many link between MarketReport and Target. The business logic is that many datapoints are associated to a specific relationship between a target and marketreport. Thanks.
Matt