Pages

Sunday, July 15, 2007

Filtering the result in hibernate

The filtering in hibernate can be acheived in two ways.
One, applying the filtering on the session before a finder or any query is hit.
Two, applying the filtering on the collection which is a persistent set after the query is hit.

For the first one,
An entry has to be made in the hbm xml's defing the filter, the parameters it take and the persistent collection on which the filter has to be applied to. Then before hitting the query on the session, we enable the filter on the session. Once the filter is enabled, any query on the session resulting in the persistent collection on which the filtering has been enable will be filtered on the filtering condition specified.

Example:

Example.hbm.xml
-------------------
<hibernate-mapping>
<class>
.
.
<set name="exampleTwo" inverse="true">
<key>
<column name="EXAMPLETWO_ID" precision="12" scale="0" />
</key>
<one-to-many class="ExampleTwo" />
<filter name="CaseOneFilter" condition="ACTIVE_IND = :CaseOneFilterParam"/>
</set>
</class>

<filter-def name="CaseOneFilter">
<filter-param name="CaseOneFilterParam" type="char"/>
</filter-def>

</hibernate-mapping>

In Code,
we enable the filter as:
session.enableFilter("CaseOneFilter").setParameter("CaseOneFilterParam",'Y');


For the second case,
Once we hit a finder and get a hook on a persistent object, we can create a filter on the session. The collection associated with this persistent object on which filtering has to be applied is sent to this newly created filter on the session. This results in getting a collection which is filtered on the conditions specified in the filter.

Example:


public static <T> Collection<T> filterCollection(Collection<T> collection, Session session,String filterCondition) {
Query filterQuery = s.createFilter(collection, filterCondition);
return filterQuery.list();
}

This can be called as:
/**
* Assuming pk to be perisitent object, session to be hibernate session.
*
*/
filterCollection(pk.getChildrens(), session, "select this.attribute where this.id = 23");

No comments:

Post a Comment