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

发布于 2024-08-22 23:35:10 字数 344 浏览 5 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-08-29 23:35:10

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

根据您最近的评论,您想要一个 三元关联,或组件集合。我已经包含了这两个映射。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文