I'm new to Hibernate and JPA. I'm trying to implement pagination with filtering. Right now for this reason I use 2 query:
query = em.createQuery("SELECT l from LoanAccount l where l."+filterColumn +" LIKE :filterValue and l.assignedBranchKey=:TaskID order by l." + orderByColumnID +" " + orrderByASC);
q = em.createQuery("SELECT count(l.id) from LoanAccount l where l."+filterColumn +" LIKE :filterValue and l.assignedBranchKey=:TaskID");
query.setParameter("filterValue", "%"+filterValue+"%");
q.setParameter("filterValue", "%"+filterValue+"%");
q.setParameter("TaskID", TaskID);
query.setParameter("TaskID", TaskID);
query.setFirstResult(page*count-count);
query.setMaxResults(count);
result.setMaxResults((Long)q.getSingleResult());
result.setData(query.getResultList());
return result;
So I'm setting first and last results in first query so I can't get list and get it's size. That's why I created second query and use it without setting first and last result - to get exact count of rows for pagination.
Page - which page is user sees now
count - how many rows should be in that page
is it possible to combine this 2 query ? if yes how should I do that?