Skip to content

Restriction predicates

gert-wijns edited this page Sep 13, 2014 · 2 revisions

Restriction predicates can be used on restriction level or on query level. Restriction predicates are used when transforming the query to an hql query. If the predicate is true, then the restriction will be included in the generated hql query.

A typical use case is when selecting a range of results based on a selector. The selector could be missing some fields because they should not be used in the query, normally we would have to wrap the query building with null or empty checks to not include these. It's easier to just set a predicate instead in such a case.

PersonSelector selector = new PersonSelector();
selector.setMinimumAge(18);
selector.setMaximumAge(null);
selector.setNames(Collections.<String>emptySet());

Person person = query.from(Person.class);
query.setDefaultRestrictionPredicate(IGNORE_NULL_OR_EMPTY);
query.where(person.getAge()).
    gt(selector.getMinimumAge()).
    lt(selector.getMaximumAge()); // null
query.where(person.getName()).in(selector.getNames()); // empty

=> "from Person hobj1 where hobj1.age > :np1" [np1=18]
Clone this wiki locally