Browse DevX
Sign up for e-mail newsletters from DevX


Hibernate Criteria API: Multi-Criteria Search Made Easy : Page 3

The Hibernate Criteria API, a powerful and flexible alternative to traditional HQL queries, makes writing all those tricky multi-criteria search functions—as well as dynamic queries in general—much easier.




Building the Right Environment to Support AI, Machine Learning and Deep Learning


The Hibernate Query API supports a rich set of comparison operators. The standard SQL operators (=, <, ≤, >, ≥ ) are supported by the following methods in the Expression class, respectively: eq(), lt(), le(), gt(), ge(). The following example uses ge() and eq():

session.createCriteria(Accommodation.class) .add(Expression.ge("availabilityDate", startDate) .list(); session.createCriteria(Accommodation.class) .add(Expression.eq("country",country)) .list();

The API also provides additional comparison operators: like, between, in, isNull, isNotNull, etc. The following example uses between, in, and like:

session.createCriteria(Accommodation.class) .add(Expression.between("availabilityDate", startDate, endDate)) .list(); session.createCriteria(Country.class) .add(Expression.like("A%")) .list(); favoriteOwners = new ArrayList(); ... session.createCriteria(Accommodation.class) .add(Expression.in("owner", favoriteOwners)) .list();

Finally, if you (really) need to, you can also build nested expressions using 'and' and 'or', as in the following example:

session.createCriteria(Accommodation.class) .add(Expression.or( Expression.eq("owner",peter), Expression.eq("owner",paul) ) .list();

In practice, I have found little use for 'and' and 'or' clauses in true dynamic queries, but who knows? You might think of one!

Ordering Results

To order your query results, you use the addOrder() method and the Order class:

session.createCriteria(Accommodation.class) .add(Expression.between("availabilityDate", startDate, endDate)) .addOrder( Order.desc("availabilityDate") ) .addOrder( Order.asc("country.name") ) .list();

The generated HQL query would be something like:

from Accommodation a order by a.availabilityDate desc, country.name asc

Joining Tables

In HQL, additional associated objects can be loaded in the same query using a "left join fetch" clause (see Sidebar 2. Hibernate Joins), as in the following example:

from Accommodation a where a.availabilityDate >=:startDate left join fetch a.country

When using the criteria API, you need to do the same thing using the setFetchMode() function:

session.createCriteria(Accommodation.class) .setFetchMode("country",FetchMode.EAGER) .list();

Sometimes you don't need to actually load the associated object (or set of objects). You just need to use one of its columns as a query criterion. Say you now need to add a language criterion in the search screen. For example, the user may want to find all accommodations where French is one of the languages spoken. To model this relation, you attach a set of languages to the Accommodation object. In HQL, the query might be the following:

from Accommodation a join a.languages language where language.code = 'FR'

Using the Criteria API, you use the createCriteria() method to create an inner join between the two tables, as in the following example:

session.createCriteria(Accommodation.class) .createCriteria("languages", "language") .add(Expression.eq("language.code", "FR")) .list();

Another way of doing this is to use the createAlias() method, which does not involve creating a new instance of the Criteria class:

session.createCriteria(Accommodation.class) .createAlias("languages", "language") .add(Expression.eq("language.code", "FR")) .list();

Note that in both these cases, the languages collection in each Accommodation object will not be initialized—it is simply used as part of the search criteria.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date