EntityManager entityManager = ... // initialize entity manager CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQueryquery = criteriaBuilder.createQuery(Customer.class); Root root = query.from(Customer.class); query.select(root); List customers = entityManager.createQuery(query).getResultList();
EntityManager entityManager = ... // initialize entity manager CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQueryThis example gets a CriteriaBuilder instance from the EntityManager and uses it to build a query that gets a single instance of the Customer entity with the name "John Doe". The javax.persistence package library contains the Java Persistence API interfaces and classes.query = criteriaBuilder.createQuery(Customer.class); Root root = query.from(Customer.class); query.select(root).where(criteriaBuilder.equal(root.get("name"), "John Doe")); Customer customer = entityManager.createQuery(query).getSingleResult();