【问题标题】:How do I map a one-to-many relationship through a join table?如何通过连接表映射一对多关系?
【发布时间】:2010-02-24 13:24:17
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 您好,Lachlan,感谢您一直以来的帮助。您最近的编辑在 Target 和 DataPoint 之间建立了直接的一对多,您将如何通过同一个连接表创建链接,即。 ReportTargets,用于 MarketReport 和 Target 之间的多对多链接。业务逻辑是许多数据点与目标和市场报告之间的特定关系相关联。谢谢。
    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多