Introduction to the Criteria API

There are two ways to write database queries in Hibernate:

  • Hibernate Query Language
  • Criteria API

You have already met the first one a long time ago, it is time to get acquainted with the Criteria API. This is a very powerful tool, at some point it was even more popular than HQL. Now it is not so popular anymore, but for some tasks it will definitely be a better solution than HQL.

In any case, you can't learn Hibernate without getting familiar with the Criteria API. Let's write a small example, and then we will analyze it. For example, we will request all employees (Employee) from the database. Here's what we'll get:

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Employee> critQuery = builder.createQuery(Employee.class);

Root<Employee> root = critQuery.from(Employee.class);;

Query<Employee> query = session.createQuery(critQuery);
List<Employee> results = query.getResultList();

Looks complicated. Let's write the same HQL query for comparison:

String hqlQuery = "from Employee";

Query<Employee> query = session.createQuery(hqlQuery);
List<Employee> results = query.getResultList();

Note that the last two lines of both examples are almost identical: we create a Query object and use it to get a List. This hints that the rest of the lines are doing something identical.

Look at lines 3 and 4 of the first example:

Root<Employee> root = critQuery.from(Employee.class);;

Let's write them in one line:;

Doesn't it remind you of anything? And if you color it a little differently:;

Yes, this is such a tricky construction of the SELECT FROM query.

Examples of working with the Criteria API

For a better understanding, I will just give a few examples.

Request 1 . Get all employees with a salary above 10 thousand:"salary"), 10000));

Request 2 . Get all employees with a salary of less than 50 thousand:"salary"), 50000));

Request 3 . Get all employees whose job title contains the word "test":"occupation"), "%test%"));

Request 4 . Get all employees with a salary of 10 to 50 thousand:"salary"), 10000, 50000));

Request 5 . Get all employees whose name is null:"name")));

Request 6 . Get all employees whose name is not null:"name")));

It's just such a tricky way to construct a query:

  • First you get the objectCriteriaBuilder.
  • Then use it to create an objectCriteriaQuery.
  • Then you start adding parts to it withCriteriaQueryAndCriteriaBuilder.

This is how you can set parameters for:

  • FROM

Also usingCriteriaBuilderyou can construct different conditions for WHERE.

Advanced work with Criteria API

Using the Criteria API, you can construct a query of any complexity. And this is great news. For example, you want a complex WHERE clause. Here's how to do it:

Predicate greaterThan ="salary"), 1000);
Predicate testers ="occupation"), "test%");, testers));

If you want to write AND instead of OR, then you only need to change the last line:, testers));

Everything is actually very simple. Let me give you a table with a few comparisons:

SQL Method Full record
a<b lt(a, b), b)
a > b gt(a, b), b)
a OR b or(a,b) builder.or(a, b)
a AND b and(a,b) builder.and(a,b)
a LIKE b like(a,b), b)
a BETWEEN (c, d) between(a, c, d) builder.between(a, c, d)
a IS NULL isNull(a) builder.isNull(a)
a IS NOT NULL isNotNull(a) builder.isNotNull(a)

Everything is simple, isn't it?

And how do we add sorting to the query? Very simple: critQuery.from(Employee.class) );
critQuery.where( builder.and(greaterThan, testers) );
critQuery.orderBy( builder.asc(root.get("salary"), builder.desc(root.get("joinDate") )

You just call on the objectCriteriaQueryorderBy() method and pass the required parameters to it.

Here's how the same query would look in HQL. Compare:

select * from Employee
where (…) and (…)
order by 'salary' asc, 'joinDate' desc

You just need to remember 3 things:

  • Key operators like SELECT, FROM, WHERE are called on an objectCriteriaQuery.
  • Auxiliary operators like AND, OR, DESC are called on the objectCriteriaBuilder.
  • Field names are taken from the object via get()root.
Module 4. Working with databases, level 16, lesson 0
Introducing the Criteria API
Module 4. Working with databases, level 16, lesson 0
Result filter in Criteria API