NHibernate集合子查询 [英] NHibernate Collection Sub Query

查看:83
本文介绍了NHibernate集合子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为NHibernate的映射添加条件?

Is there a way to add criteria to a mapping for NHibernate?

public class Course {
   public IList<Participant> Participants { get; set; }
   public IList<Participant> ActiveParticipants { get; set; }
}

第二个属性ActiveParticipants应该是参与者的状态"设置为活动"的列表.否则,有一种方法可以对类进行后处理,但仍保持延迟加载,因为有时将不使用"ActiveParticipants"属性.

The second property, ActiveParticipants should be a list of participants where their "Status" is set to "Active". Otherwise, is there a way to post process the class, but still maintaining Lazy Loading, as sometimes the "ActiveParticipants" property will not be used.

之所以需要这样而不是使用LINQ,是因为该类被注入到动态"Mustache"模板中,该模板仅适用于标准属性.也有一些类似的属性.

The reason it needs to be like this instead of using LINQ, is the class is injected into a dynamic "Mustache" template, which only works with standard properties. There are also several similar properties like this.

推荐答案

在这种情况下,我们似乎需要动态"过滤.映射的集合有时会包含所有记录,有时只是那些满足某些过滤条件的记录.

It seems, that in this case, we would need "dynamic" filtering. The mapped collection will sometimes contain contain all records, sometimes just those which meet some filtering criteria.

为此,我们有:

NHibernate添加了预定义过滤器条件并将这些过滤器附加到类和集合级别的功能.过滤条件是一种定义限制子句的能力,该限制子句与类和各种集合元素上可用的现有"where"属性非常相似.除了这些过滤条件,可以进行参数化.然后,应用程序可以在运行时确定是否应启用给定的过滤器以及其参数值应为什么.过滤器可以像数据库视图一样使用,但是可以在应用程序内部进行参数化.

NHibernate adds the ability to pre-define filter criteria and attach those filters at both a class and a collection level. A filter criteria is the ability to define a restriction clause very similiar to the existing "where" attribute available on the class and various collection elements. Except these filter conditions can be parameterized. The application can then make the decision at runtime whether given filters should be enabled and what their parameter values should be. Filters can be used like database views, but parameterized inside the application.

为了使用过滤器,必须首先定义它们,然后将其附加到适当的映射元素上.要定义过滤器,请在元素内使用元素:

In order to use filters, they must first be defined and then attached to the appropriate mapping elements. To define a filter, use the element within a element:

<filter-def name="myFilter">
    <filter-param name="myFilterParam" type="String"/>
</filter-def>

然后,此过滤器可以附加到一个类上:

Then, this filter can be attached to a class:

<class name="MyClass" ...>
    ...
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</class>

或者,到一个集合:

or, to a collection:

<set ...>
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</set>

在此处查看更多详细信息(xml)

Check more details here (xml)

  • Is it possible to use NHibernate Filters to filter through references?
  • NHibernate load entity with part of sub collection

(流利的映射)

这篇关于NHibernate集合子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆