如何通过连接表映射一对多关系?

时间:2010-02-24 13:24:17

标签: nhibernate nhibernate-mapping one-to-many

我如何在NHibernate中映射以下内容?
我的实体和ERD如下。我知道如何映射多对多关系,但不知道如何将连接表ReportTargets映射到Datapoint表。您会注意到没有ReportTargets实体模型,因为它不是严格意义上的域实体。这里最好的解决方案是什么?我是一个NHibernate新手所以请轻松请... :)谢谢

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

1 个答案:

答案 0 :(得分:1)

由于MarketReport.Targets有一个连接表,请将其映射为多对多。

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

根据您最近的评论,您希望获得ternary associationcomponent collection。我已经包含了两个映射。

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