Hibernate过滤器使用窍门

开发 后端
Hibernate过滤器(filter)是全局有效的、具有名字、可以带参数的过滤器,对于某个特定的Hibernate session您可以选择是否启用(或禁用)某个过滤器。

本文向大家介绍Hibernate过滤器,可能好多人还不了解Hibernate过滤器,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

Hibernate3新增了对某个类或者集合使用预先定义的Hibernate过滤器条件(filter criteria)的功能。过滤器条件相当于定义一个 非常类似于类和各种集合上的“where”属性的约束子句,但是过滤器条件可以带参数。

应用程序可以在运行时决定是否启用给定的Hibernate过滤器,以及使用什么样的参数值。 过滤器的用法很像数据库视图,只不过是在应用程序中确定使用什么样的参数的。

要使用过滤器,必须首先在相应的映射节点中定义。而定义一个过滤器,要用到位于<hibernate-mapping/> 节点之内的<filter-def/>节点:

<filter-def name="myFilter"> 
 
    <filter-param name="myFilterParam" type="string"/> 
 
</filter-def> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

定义好之后,就可以在某个类中使用这个过滤器:

<class name="myClass" ...> 
 
    ...  
 
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/> 
 
</class> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

也可以在某个集合使用它:

<set ...> 
 
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/> 
 
</set> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

可以在多个类或集合中使用某个过滤器;某个类或者集合中也可以使用多个过滤器。

Session对象中会用到的方法有:enableFilter(String filterName), getEnabledFilter(String filterName), 和 disableFilter(String filterName). Session中默认是不启用过滤器的,必须通过Session.enabledFilter()方法显式的启用。 该方法返回被启用的Filter的实例。以上文定义的过滤器为例:

session.enableFilter("myFilter").setParameter("myFilterParam", "some-value"); 
  • 1.

注意,org.hibernate.Filter的方法允许链式方法调用。(类似上面例子中启用Filter之后设定Filter参数这个“方法链”) Hibernate的其他部分也大多有这个特性。

下面是一个比较完整的例子,使用了记录生效日期模式过滤有时效的数据:

 

<filter-def name="effectiveDate"> 
 
    <filter-param name="asOfDate" type="date"/> 
 
</filter-def> 
 
 
 
<class name="Employee" ...> 
 
...  
 
    <many-to-one name="department" column="dept_id" class="Department"/> 
 
    <property name="effectiveStartDate" type="date" column="eff_start_dt"/> 
 
    <property name="effectiveEndDate" type="date" column="eff_end_dt"/> 
 
...  
 
    <!--  
 
        Note that this assumes non-terminal records have an eff_end_dt set to  
 
        a max db date for simplicity-sake  
 
 
 
        注意,为了简单起见,此处假设雇用关系生效期尚未结束的记录的eff_end_dt字段的值等于数据库最大的日期  
 
    --> 
 
    <filter name="effectiveDate" 
 
            condition=":asOfDate BETWEEN eff_start_dt and eff_end_dt"/> 
 
</class> 
 
 
 
<class name="Department" ...> 
 
...  
 
    <set name="employees" lazy="true"> 
 
        <key column="dept_id"/> 
 
        <one-to-many class="Employee"/> 
 
        <filter name="effectiveDate" 
 
                condition=":asOfDate BETWEEN eff_start_dt and eff_end_dt"/> 
 
    </set> 
 
</class> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.

定义好后,如果想要保证取回的都是目前处于生效期的记录,只需在获取雇员数据的操作之前先开启过滤器即可:

Session session = ...;  
 
session.enabledFilter("effectiveDate").setParameter("asOfDate", new Date());  
 
List results = session.createQuery("from Employee as e where e.salary > :targetSalary")  
 
         .setLong("targetSalary", new Long(1000000))  
 
         .list();  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

在上面的HQL中,虽然我们仅仅显式的使用了一个薪水条件,但因为启用了过滤器,查询将仅返回那些目前雇用 关系处于生效期的,并且薪水高于一百万美刀的雇员的数据。

注意:

如果你打算在使用外连接(或者通过HQL或load fetching)的同时使用过滤器,要注意条件表达式的方向(左还是右)。 最安全的方式是使用左外连接(left outer joining)。并且通常来说,先写参数, 然后是操作符,最后写数据库字段名。

【编辑推荐】

  1. 浅述Hibernate性能优化
  2. Hibernate传播性持久化攻略
  3. 深索Hibernate延迟加载技术
  4. 深入了解Hibernate自动状态检测
  5. Hibernate扩展周期的Session和自动版本化
责任编辑:仲衡 来源: IT168
相关推荐

2009-09-29 13:55:23

Hibernate设置

2009-06-18 10:13:00

Hibernate过滤

2024-01-05 09:04:35

隆过滤器数据结构哈希函数

2021-07-05 15:22:03

Servlet过滤器客户端

2024-11-04 08:45:48

布隆过滤器元数据指纹值

2009-07-08 16:07:04

Servlet过滤器配

2009-07-14 09:09:08

Swing模型过滤器

2009-07-08 15:30:56

Servlet过滤器

2011-06-29 16:14:59

Qt 事件 过滤器

2025-04-21 00:50:50

2022-10-11 23:14:39

模态循环过滤器代码

2017-07-18 14:10:31

大数据Apache Flum过滤器

2009-07-08 17:33:37

Servlet过滤器

2020-10-29 07:16:26

布隆过滤器场景

2024-03-15 11:21:22

布隆过滤器数据库数据

2009-07-06 13:02:49

Servlet过滤器

2023-01-26 01:41:27

核心全局过滤器

2016-12-07 09:56:13

JavaFilter过滤器

2017-04-12 14:43:01

Spring ClouZuul过滤器

2010-03-01 14:45:07

Linux文件重定向
点赞
收藏

51CTO技术栈公众号