Hibernate: Specification is the Spring Data wrapper around Criteria

Composable predicates that a repository can accept directly.

Code
interface OrderRepository extends JpaRepository<Order, Long>,
                                  JpaSpecificationExecutor<Order> { }

static Specification<Order> hasStatus(Status s) {
    return (root, q, cb) -> cb.equal(root.get("status"), s);
}
static Specification<Order> over(BigDecimal amount) {
    return (root, q, cb) -> cb.gt(root.get("total"), amount);
}

repo.findAll(hasStatus(PLACED).and(over(new BigDecimal("100"))));
Output
select ... from orders where status=? and total>?
Each condition is a reusable, testable unit.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11