Hello,
I have a question about Parameters
of object Query
on JPA
.
I am implementing pagination of my tables using Primefaces
with LazyDataModel
and in the function that I use to perform pagination I do 2 searches in the database. The first one to filter the data and return only those of the range q I need. And the second to return the COUNT
of the total records to the filter (without the range limit).
The only difference between these queries is that the first one has a maxResults = range[1] - range[0]
and a firstResult = range[0]
. The actually returning only the results I want from the base.
So, I'd like to be able to reuse the filter parameters of the first Query
, in the second.
Something like this:
Query queryUm = em.createQuery("SELECT x FROM...")
.setParameter("x", 1)
.setParameter("x", 1)
.setParameter("x", 1)
.setMaxResults(range[1] - range[0])
.setFirstResult(range[0]);
List<Entidade> list = queryUm.getResultList();
Query queryDois = em.createQuery("SELECT COUNT(x) From...");
//reutiliza os parametros aqui..
queryDois.getParameters().addAll(queryUm.getParameters());
Long totalRecords = queryDois.getSingleResult();
I have tried to do something like this, but the second Query
does not execute with an error saying that the expected parameter was not found.
I look forward to your contributions, Thank you.