0

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

http://img341.imageshack.us/img341/3769/entities.gif

4

1 回答 1

1

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>
于 2010-02-24T14:01:07.217 回答