In JPA, Predicate is the interface that represents a single boolean condition in the Criteria API. It is what you pass to where() and what CriteriaBuilder methods such as equal, greaterThan, like, and, or and not return. Because predicates are ordinary Java objects, you can build them in a list at runtime and combine them, which makes dynamic filters type-safe instead of string concatenation.
The interface is jakarta.persistence.criteria.Predicate. In JPA 2.x and Java EE it was javax.persistence.criteria.Predicate. Predicate has been part of JPA since version 2.0.
A Predicate extends Expression<Boolean>. It models one condition of a WHERE, HAVING or ON clause, such as age > 25 or status = 'ACTIVE'. It can be simple or compound, and a compound predicate has a boolean operator (AND or OR) that you can read with getOperator(). You can negate it with isNegated() or not().
You never implement Predicate yourself. You get instances from a CriteriaBuilder, which is obtained from the EntityManager.
The generated SQL is roughly select ... from customer c where c.age > ? and c.status = ?, and the values are bound as parameters, not concatenated. Note that cb.gt is for numeric expressions only. For general comparable values such as strings and dates, use greaterThan, lessThan and between.
The main reason to use Predicate is to build filters from optional input. Collect predicates in a list and add only those the user supplied.
If the list is empty, cb.and() with no arguments produces a conjunction that is always true, so the query returns all rows. Use cb.or(...) for alternatives and cb.not(predicate) or predicate.not() for negation.
| Method | SQL meaning |
|---|---|
equal, notEqual | =, <> |
greaterThan, lessThan, between | >, <, BETWEEN |
like, notLike | LIKE |
isNull, isNotNull | IS NULL, IS NOT NULL |
in(...) via root.get("x").in(values) | IN (...) |
isTrue, isFalse | boolean column checks |
exists(subquery) | EXISTS |
and, or, not | logical combination |
Spring Data JPA wraps this same API in Specification<T>. A specification is a function that returns a Predicate, and specifications can be chained with and and or.
The repository must extend JpaSpecificationExecutor<Customer>. Returning cb.conjunction() means no restriction, which keeps optional filters tidy.
String names such as "age" are checked only at runtime. The JPA static metamodel generates classes like Customer_ so that attribute names are checked by the compiler:
Add the Hibernate JPA Metamodel Generator (or your provider's equivalent) as an annotation processor. A renamed field then breaks compilation instead of failing in production.
like patterns: escape % and _ in user input if they should be literal.No. The JPA Predicate is a query condition that is translated to SQL, while java.util.function.Predicate is an in-memory functional interface. They share only the name.
Use cb.or(p1, p2) or the list form cb.or(list.toArray(new Predicate[0])).
An empty cb.and() is always true. That is usually intended, but add a page limit with setMaxResults to avoid loading a whole table.
Yes. Hibernate 6 and later implement Jakarta Persistence 3.x, and the Criteria API is unchanged apart from the jakarta package.
A JPA Predicate is one condition of a Criteria query. Create it with CriteriaBuilder, combine predicates with and, or and not, collect them in a list for dynamic filters and use the static metamodel for compile-time safety. Spring Data's Specification is built on the same idea.