To prepare a query for execution, create a TypedQuery<T> object with the type of the query result by passing the CriteriaQuery object to EntityManager.createQuery.
Queries are executed by calling either getSingleResult or getResultList on the TypedQuery<T> object.
The TypedQuery<T>.getSingleResult method is used for executing queries that return a single result.
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); ... TypedQuery<Pet> q = em.createQuery(cq); Pet result = q.getSingleResult();
The TypedQuery<T>.getResultList method is used for executing queries that return a collection of objects.
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); ... TypedQuery<Pet> q = em.createQuery(cq); List<Pet> results = q.getResultList();