Skip to main content

Criteria API and Named Queries

Learn Criteria API and Named Queries (Phase 5 · Hibernate Querying (Core), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Criteria API • The Criteria API builds queries programmatically and type-safely : CriteriaBuilder → CriteriaQuery<T> → Root<E> → Predicate s. Best for dynamic queries (search forms with optional filters) where string concatenation would be error-prone. • It is verbose; combine with the JPA static metamodel ( Course_.title , generated by hibernate-jpamodelgen ) for compile-time safety. Spring Data's Specifications wrap it nicely. (The old session.createCriteria() is removed in Hibernate 6.)

More lessons in Phase 5 · Hibernate Querying (Core)