Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Ioannis Angelis
Added: Sep 14, 2021 5:25 PM
Views: 160
Tags: criteria find jpa
  1.     public List<City> findCitiesEntities() {
  2.         return findCitiesEntities(true, -1, -1);
  3.     }
  4.  
  5.     public List<City> findCitiesEntities(int maxResults, int firstResult) {
  6.         return findCitiesEntities(false, maxResults, firstResult);
  7.     }
  8.  
  9.     private List<City> findCitiesEntities(boolean all, int maxResults, int firstResult) {
  10.         EntityManager em = getEntityManager();
  11.         try {
  12.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  13.             cq.select(cq.from(City.class));
  14.             Query q = em.createQuery(cq);
  15.             if (!all) {
  16.                 q.setMaxResults(maxResults);
  17.                 q.setFirstResult(firstResult);
  18.             }
  19.             return q.getResultList();
  20.         } finally {
  21.             em.close();
  22.         }
  23.     }