NHibernate - 如何使用 hbm 文件映射从数据库获取条件数据

发布于 2024-09-10 10:38:52 字数 954 浏览 1 评论 0原文

目前我正在 hbm 文件中使用 多对一 元素从数据库中获取数据对象,如下所示......

<property name="ContactId" length="4" />
<many-to-one
    name="DefaultContact"
    column="ContactId"
    class="Models.Contact"
    update="false"
    insert="false"/>

此代码正确获取数据,但现在我需要有条件地获取数据就像我在 hbm 文件中具有以下属性和 mant-to-one 元素......

<property name="ParentId" length="4" />
<property name="ParentType" length="4" />
<many-to-one
        name="ContactParent"
        column="???????? ParentId which could be CustomerId or ProspectId or LeadId according to Parent Type ????????"
        class="???????? Models.Customer or Models.Prospect or Models.Lead - according to Parent Type ????????"
        update="false"
        insert="false"/>

并且我必须根据“Parent Type”属性中的值获取数据,这意味着我需要设置“many”的类属性-to-one”元素根据“父类型”动态变化 财产。

那么现在,我怎样才能通过多对一元素或其他方式达到预期的结果......?

提前致谢。

Currently I am using many-to-one element in hbm file to fetch the data object from database like following....

<property name="ContactId" length="4" />
<many-to-one
    name="DefaultContact"
    column="ContactId"
    class="Models.Contact"
    update="false"
    insert="false"/>

This code is fetching the data properly, but now I need to fetch the data conditionally like I am having the following properties and mant-to-one element in hbm file.....

<property name="ParentId" length="4" />
<property name="ParentType" length="4" />
<many-to-one
        name="ContactParent"
        column="???????? ParentId which could be CustomerId or ProspectId or LeadId according to Parent Type ????????"
        class="???????? Models.Customer or Models.Prospect or Models.Lead - according to Parent Type ????????"
        update="false"
        insert="false"/>

And I have to fetch the data according to value in "Parent Type" property, which means I need to set class attribute of "many-to-one" element dynamically according to "Parent Type"
property.

So now, how can I achieve the desired result with many-to-one element or some other way...?

Thanks in Advance.

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

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

发布评论

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

评论(2

最丧也最甜 2024-09-17 10:38:52

您应该查看 映射 http://nhibernate.info/doc/nh/en/index.html#mapping-types-anymapping

这是 NHibernate 提供的最接近您想要做的事情。

You should look into the <any> mapping http://nhibernate.info/doc/nh/en/index.html#mapping-types-anymapping

It's the closest to what you want to do that NHibernate offers.

甜点 2024-09-17 10:38:52

我可以想到两种方法来处理这个问题。

  1. 不要在模型中包含 ContactParent,使用单独的方法从 ParentType 和 ParentId 检索它。

    不要在模型

  2. 将所有三个父类型映射为私有成员,并返回公共属性中不为 null 的成员。

选项 1 将是我的第一选择。

I can think of two ways to handle this.

  1. Don't include ContactParent in the model, use a separate method to retrieve it from ParentType and ParentId.

  2. Map all three parent types as private members and return the one that's not null in a public property.

Option 1 would be my first choice.

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