Finally, I am giving a try to the new feature of dynamic filters in Hibernate3.
But I have realized that when setting the parameters for the filter, the fact that a filter will not be used is done by simply not setting its parameter. For example:
<filter-def name="filterName"> <filter-param name="likeName" type="string" /> </filter-def> <filter-def name="filterSurname"> <filter-param name="likeSurname" type="string" /> </filter-def>
And then in code:
session.enableFilter("filterName").setParameter("likeName", "%John%"); session.enableFilter("filterSurname").setParameter("likeSurname", "%Doe%");
So if I don't have a value for, imagine, surname, then I just have to omit the:
session.enableFilter("filterSurname").setParameter("likeSurname", "%Doe%");
But this will lead to having a bunch of if statements asking for the possible values of the parameters, won't it?:
if (IHaveValueForName) session.enableFilter("filterName").setParameter("likeName", "%John%"); if (IHaveValueForSurname) session.enableFilter("filterSurname").setParameter("likeSurname", "%Doe%");
Wouldn't it be easier just to set the parameter always, so if it is null then Hibernate just ignores it? For example:
session.enableFilter("filterName").setParameter("likeName", "%John%"); session.enableFilter("filterSurname").setParameter("likeSurname", null);
Because this doesn't work :-(, as Hibernate throws the following exception:
java.lang.IllegalArgumentException: Filter [filterSurname] defined parameter [likeSurname] whose value was not set
|