映射 NHibernate 的反面协会

发布于 2024-09-25 21:08:19 字数 1478 浏览 2 评论 0 原文

Ayende 有一个使用 映射 此处,由于该博文的评论已关闭,我将其作为我的问题的一部分重新发布。鉴于他的原始映射:

<class name="Order" table="Orders">

    <id name="Id">
        <generator class="native"/>
    </id>

    <any name="Payment" id-type="System.Int64" meta-type="System.String" cascade="all">
        <meta-value value="CreditCard" class="CreditCardPayment"/>
        <meta-value value="Wire" class="WirePayment"/>
        <column name="PaymentType"/>
        <column name="PaymentId"/>
    </any>

</class>

<class name="CreditCardPayment" table="CreditCardPayments">
    <id name="Id">
        <generator class="native"/>
    </id>
    <property name="IsSuccessful"/>
    <property name="Amount"/>
    <property name="CardNumber"/>
</class>

<class name="WirePayment" table="WirePayments">
    <id name="Id">
        <generator class="native"/>
    </id>
    <property name="IsSuccessful"/>
    <property name="Amount"/>
    <property name="BankAccountNumber"/>
</class>

...我将如何在 CreditCardPayment 和 WirePayment 类上映射名为 Order 的属性,这将是关联的“另一面”,允许从这些付款遍历回它们关联的 Order?

对我来说,这里的问题是 CreditCardPayment 和 WirePayment 可能具有相同的 ID,因为它们位于不同的表中,因此我需要某种方法来告诉 NHibernate 将 PaymentType 考虑在内。

Ayende has a great example of using the <any> mapping here, which I am reposting as part of my question since comments are closed on that blog post. Given his original mapping:

<class name="Order" table="Orders">

    <id name="Id">
        <generator class="native"/>
    </id>

    <any name="Payment" id-type="System.Int64" meta-type="System.String" cascade="all">
        <meta-value value="CreditCard" class="CreditCardPayment"/>
        <meta-value value="Wire" class="WirePayment"/>
        <column name="PaymentType"/>
        <column name="PaymentId"/>
    </any>

</class>

<class name="CreditCardPayment" table="CreditCardPayments">
    <id name="Id">
        <generator class="native"/>
    </id>
    <property name="IsSuccessful"/>
    <property name="Amount"/>
    <property name="CardNumber"/>
</class>

<class name="WirePayment" table="WirePayments">
    <id name="Id">
        <generator class="native"/>
    </id>
    <property name="IsSuccessful"/>
    <property name="Amount"/>
    <property name="BankAccountNumber"/>
</class>

... how would I go about mapping a property named Order on the CreditCardPayment and WirePayment classes which would be the "flip side" of the association allowing traversal from these payments back up to the Order they are associated with?

The gotcha for me here is that CreditCardPayment and WirePayment can potentially have the same IDs since they are in different tables, so I need some way to tell NHibernate to take the PaymentType into consideration.

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

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

发布评论

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

评论(1

寻找一个思念的角度 2024-10-02 21:08:19

NHibernate 不支持双向异构关联(即 )。

我知道这不是你想听的,但仅此而已。

Bidirectional heterogeneous associations (i.e. <any>) are not supported by NHibernate.

I know this isn't what you wanted to hear, but that's it.

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