如何将此条件写入 NHibernate HBM 映射?
我正在参加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信在这种情况下子选择可能会起作用(假设您的 RDMS 支持它);比如:
我不太清楚你想在这里实现什么目标;但您可以考虑将其映射为仅查询属性;正如这篇文章中讨论的。
I believe that a sub-select may work in this case (assuming your RDMS supports it); so something like:
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.