如何将此条件写入 NHibernate HBM 映射?

发布于 2024-09-15 08:24:24 字数 942 浏览 2 评论 0原文

我正在参加 NHibernate 速成课程,并且在下面的示例中遇到了一些障碍。

假设我有以下 .NET 类:

class A {
    int id;
    int type_var;
    List<B> someCollection;
}

class B {
    int id;
    string someText;
}

我可能会像这样映射它:

<class name="A" table="A">
    <id name="id" type="Int32">
         <generator type="identity" />
    </id>
    <property name="type_var" />
    <set name="someCollection" table="B">
         <key name="fk_aid" />
         <composite-element class="B">
              <property name="someText" />
         </composite-element>
    </set>
</class>

我的问题是,如果我们只对获取属于 A 的 B 元素(通过 fk_aid)并且具有 type_var 值感兴趣,您将如何更改此映射等于 A(假设 A 和 B 都有 type_var 列,但它们没有显式相关)。

我想我必须在这里使用类似 where 子句的东西?

<set name="someCollection" table="B" where="type_var = type_var">

究竟该如何做呢?

So I'm on an NHibernate crash course, and kinda hit a snag with the example below.

Suppose I have the following .NET class:

class A {
    int id;
    int type_var;
    List<B> someCollection;
}

class B {
    int id;
    string someText;
}

I'll probably map it like :

<class name="A" table="A">
    <id name="id" type="Int32">
         <generator type="identity" />
    </id>
    <property name="type_var" />
    <set name="someCollection" table="B">
         <key name="fk_aid" />
         <composite-element class="B">
              <property name="someText" />
         </composite-element>
    </set>
</class>

My problem is, how would you change this mapping if we were only interested in getting those B elements that belong to A (via fk_aid) AND has a type_var value equal to A (let's assume that both A and B have a type_var column, but they're not explicitly related).

I'm thinking that I'll have to work with something like a where clause here?

<set name="someCollection" table="B" where="type_var = type_var">

How exactly would one do this?

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

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

发布评论

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

评论(1

梨涡 2024-09-22 08:24:24

我相信在这种情况下子选择可能会起作用(假设您的 RDMS 支持它);比如:

<set name="someCollection" table="B" where="type_var = (SELECT A.type_var FROM A WHERE A.fk_aid = id)"> 

我不太清楚你想在这里实现什么目标;但您可以考虑将其映射为仅查询属性;正如这篇文章中讨论的

I believe that a sub-select may work in this case (assuming your RDMS supports it); so something like:

<set name="someCollection" table="B" where="type_var = (SELECT A.type_var FROM A WHERE A.fk_aid = id)"> 

I'm not overly clear on what you're trying to achieve here; but you may consider mapping this as a query-only property; as discussed in this article.

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