📄 filters.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>第 18 章 过滤数据</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - 符合Java习惯的关系数据库持久化"><link rel="up" href="index.html" title="HIBERNATE - 符合Java习惯的关系数据库持久化"><link rel="previous" href="querysql.html" title="第 17 章 Native SQL查询"><link rel="next" href="xml.html" title="第 19 章 XML映射"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">第 18 章 过滤数据</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="querysql.html">上一页</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="xml.html">下一页</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="filters"></a>第 18 章 过滤数据</h2></div></div><div></div></div><p> Hibernate3 提供了一种创新的方式来处理具有“显性(visibility)”规则的数据,那就是使用<span class="emphasis"><em>Hibernate filter</em></span>。 <span class="emphasis"><em>Hibernate filter</em></span>是全局有效的、具有名字、可以带参数的过滤器, 对于某个特定的Hibernate session您可以选择是否启用(或禁用)某个过滤器。 </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="objectstate-filters"></a>18.1. Hibernate 过滤器(filters)</h2></div></div><div></div></div><p> Hibernate3新增了对某个类或者集合使用预先定义的过滤器条件(filter criteria)的功能。过滤器条件相当于定义一个 非常类似于类和各种集合上的“where”属性的约束子句,但是过滤器条件可以带参数。 应用程序可以在运行时决定是否启用给定的过滤器,以及使用什么样的参数值。 过滤器的用法很像数据库视图,只不过是在应用程序中确定使用什么样的参数的。 </p><p> 要使用过滤器,必须首先在相应的映射节点中定义。而定义一个过滤器,要用到位于<tt class="literal"><hibernate-mapping/></tt> 节点之内的<tt class="literal"><filter-def/></tt>节点: </p><pre class="programlisting"><filter-def name="myFilter"> <filter-param name="myFilterParam" type="string"/></filter-def></pre><p> 定义好之后,就可以在某个类中使用这个过滤器: </p><pre class="programlisting"><class name="myClass" ...> ... <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/></class></pre><p> 也可以在某个集合使用它: </p><pre class="programlisting"><set ...> <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/></set></pre><p> 可以在多个类或集合中使用某个过滤器;某个类或者集合中也可以使用多个过滤器。 </p><p> <tt class="literal">Session</tt>对象中会用到的方法有:<tt class="literal">enableFilter(String filterName)</tt>, <tt class="literal">getEnabledFilter(String filterName)</tt>, 和 <tt class="literal">disableFilter(String filterName)</tt>. Session中默认是<span class="emphasis"><em>不</em></span>启用过滤器的,必须通过<tt class="literal">Session.enabledFilter()</tt>方法显式的启用。 该方法返回被启用的<tt class="literal">Filter</tt>的实例。以上文定义的过滤器为例: </p><pre class="programlisting">session.enableFilter("myFilter").setParameter("myFilterParam", "some-value");</pre><p> 注意,org.hibernate.Filter的方法允许链式方法调用。(类似上面例子中启用Filter之后设定Filter参数这个“方法链”) Hibernate的其他部分也大多有这个特性。 </p><p> 下面是一个比较完整的例子,使用了记录生效日期模式过滤有时效的数据: </p><pre class="programlisting"><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></pre><p> 定义好后,如果想要保证取回的都是目前处于生效期的记录,只需在获取雇员数据的操作之前先开启过滤器即可: </p><pre class="programlisting">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();</pre><p> 在上面的HQL中,虽然我们仅仅显式的使用了一个薪水条件,但因为启用了过滤器,查询将仅返回那些目前雇用 关系处于生效期的,并且薪水高于一百万美刀的雇员的数据。 </p><p> 注意:如果你打算在使用外连接(或者通过HQL或load fetching)的同时使用过滤器,要注意条件表达式的方向(左还是右)。 最安全的方式是使用左外连接(left outer joining)。并且通常来说,先写参数, 然后是操作符,最后写数据库字段名。 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="querysql.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="xml.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">第 17 章 Native SQL查询 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 第 19 章 XML映射</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -