Java Reference
In-Depth Information
filter would specify that the status column must match a named parameter. You would not
need to define the possible values of the status column in the Hibernate mapping docu-
ment—the application can specify those parameters at run time.
Although it is certainly possible to write applications with Hibernate that do not use fil-
ters, we find them to be an excellent solution to certain types of problems—notably security
and personalization.
Defining Filters
Your first step is to define filters in your application's Hibernate mapping documents, using
the <filter-def> XML element. These filter definitions must contain the name of the filter
and the names and types of any filter parameters. Specify filter parameters with the
<filter-param> XML element. Filter parameters are similar to named parameters for HQL
queries. Both require a : before the parameter name. Here is an excerpt from a mapping docu-
ment with a filter called latePaymentFilter defined:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class ...
</class>
<filter-def name="latePaymentFilter">
<filter-param name="dueDate" type="date"/>
</filter-def>
</hibernate-mapping>
Once you have created the filter definitions, you need to attach the filters to class or col-
lection mapping elements. You can attach a single filter to more than one class or collection.
To do this, you add a <filter> XML element to each class and/or collection. The <filter>
XML element has two attributes: name and condition . The name references a filter definition
(for instance: latePaymentFilter ). The condition represents a WHERE clause in HQL. Here's an
example:
<class ...
<filter name="latePaymentFilter" condition=":dueDate = paymentDate"/>
</class>
Each <filter> XML element must correspond to a <filter-def> element. You may have
more than one filter for each filter definition, and each class can have more than one filter.
This is a little confusing—the extra level of abstraction allows you to define all the filter
parameters in one place and then refer to them in the individual filter conditions.
Search WWH ::




Custom Search