Criteria API
The Jakarta Persistence Criteria API is meant as a complement to JPQL. The Criteria API allows us to write Jakarta Persistence queries programmatically, without having to rely on JPQL.
The Criteria API offers a few advantages over JPQL – for instance, JPQL queries are stored as Strings, and the compiler has no way of validating JPQL syntax. Additionally, JPQL is not type safe; we could write a JPQL query in which our where
clause could have a string value for a numeric property, and our code would compile and deploy just fine.
The following code example illustrates how to use the Criteria API in our Jakarta EE applications:
package com.ensode.jakartaeebook.criteriaapi.namedbean; //imports omitted for brevity @Named @RequestScoped public class CriteriaApiDemoBean { @PersistenceContext private EntityManager entityManager; private List<UsState> matchingStatesList; public String findStates() { ...