Hibernate.orgCommunity Documentation
Table of Contents
Hibernate provides an innovative new approach to handling data with "visibility" rules. A Hibernate filter is a global, named, parameterized filter that can be enabled or disabled for a particular Hibernate session.
Hibernate has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be. Filters can be used like database views, but they are parameterized inside the application.
Using annotatons filters are defined via
@org.hibernate.annotations.FilterDef
or
@org.hibernate.annotations.FilterDefs
. A filter
definition has a name()
and an array of
parameters(). A parameter will allow you to adjust the behavior of the
filter at runtime. Each parameter is defined by a
@ParamDef
which has a name and a type. You can also
define a defaultCondition()
parameter for a given
@FilterDef
to set the default condition to use when
none are defined in each individual @Filter
.
@FilterDef
(s) can be defined at the class or package
level.
We now need to define the SQL filter clause applied to either the
entity load or the collection load. @Filter
is used and
placed either on the entity or the collection element. The connection
between @FilterName
and
@Filter
is a matching name.
Example 19.1. @FilterDef and @Filter annotations
@Entity
@FilterDef(name="minLength", parameters=@ParamDef( name="minLength", type="integer" ) )
@Filters( {
@Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length"),
@Filter(name="minLength", condition=":minLength <= length")
} )
public class Forest { ... }
When the collection use an association table as a relational
representation, you might want to apply the filter condition to the
association table itself or to the target entity table. To apply the
constraint on the target entity, use the regular
@Filter
annotation. However, if you want to target the
association table, use the @FilterJoinTable
annotation.
Example 19.2. Using @FilterJoinTable
for filterting on
the association table
@OneToMany
@JoinTable
//filter on the target entity table
@Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length")
//filter on the association table
@FilterJoinTable(name="security", condition=":userlevel >= requredLevel")
public Set<Forest> getForests() { ... }
By default, Hibernate attempts to automatically determine all points within the
@Filter
SQL condition fragment that an alias should be injected. To control the alias injection,
set deduceAliasInjectionPoints
to false
within the
@Filter
. Injection points are then marked using @SqlFragmentAlias
annotations or
within the SQL's condition fragment using {alias}
.
In addition to allowing explicit alias control, deduceAliasInjectionPoints
provides an out when Hibernate assumes an ANSI SQL reserved keyword is a column and incorrectly aliases it.
Example 19.3. @Filter annotation, disabling deduceAliasInjectionPoints
@Entity
@Table(name="T_TREE")
@Filters({
@Filter(name="isTall", condition="{alias}.LENGTH >= 100", deduceAliasInjectionPoints = false),
@Filter(name="isOak", condition="{t}.WOODTYPE like 'oak'", deduceAliasInjectionPoints = false,
aliases={@SqlFragmentAlias(alias="t", table="T_TREE")})
})
public class Tree { ... }
Using Hibernate mapping files for defining filters the situtation is
very similar. The filters must first be defined and then attached to the
appropriate mapping elements. To define a filter, use the
<filter-def/>
element within a
<hibernate-mapping/>
element:
Example 19.4. Defining a filter definition via
<filter-def>
<filter-def name="myFilter"> <filter-param name="myFilterParam" type="string"/> </filter-def>
This filter can then be attached to a class or collection (or, to both or multiples of each at the same time):
Example 19.5. Attaching a filter to a class or collection using
<filter>
<class name="myClass" ...> ... <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/> <set ...> <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/> </set> </class>
The methods on Session
are:
enableFilter(String filterName)
,
getEnabledFilter(String filterName)
, and
disableFilter(String filterName)
. By default, filters
are not enabled for a given session. Filters must be
enabled through use of the Session.enableFilter()
method, which returns an instance of the Filter
interface. If you used the simple filter defined above, it would look like
this:
session.enableFilter("myFilter").setParameter("myFilterParam", "some-value");
Methods on the org.hibernate.Filter interface do allow the method-chaining common to much of Hibernate.
The following is a full example, using temporal data with an effective record date pattern:
<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 --> <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>
In order to ensure that you are provided with currently effective records, enable the filter on the session prior to retrieving employee data:
Session session = ...; session.enableFilter("effectiveDate").setParameter("asOfDate", new Date()); List results = session.createQuery("from Employee as e where e.salary > :targetSalary") .setLong("targetSalary", new Long(1000000)) .list();
Even though a salary constraint was mentioned explicitly on the results in the above HQL, because of the enabled filter, the query will return only currently active employees who have a salary greater than one million dollars.
If you want to use filters with outer joining, either through HQL or load fetching, be careful of the direction of the condition expression. It is safest to set this up for left outer joining. Place the parameter first followed by the column name(s) after the operator.
After being defined, a filter might be attached to multiple entities
and/or collections each with its own condition. This can be problematic
when the conditions are the same each time. Using
<filter-def/>
allows you to definine a default
condition, either as an attribute or CDATA:
<filter-def name="myFilter" condition="abc > xyz">...</filter-def> <filter-def name="myOtherFilter">abc=xyz</filter-def>
This default condition will be used whenever the filter is attached to something without specifying a condition. This means you can give a specific condition as part of the attachment of the filter that overrides the default condition in that particular case.