且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NHibernate集合子查询

更新时间:2022-11-27 13:27:41

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

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

(流利的映射)