0
Hello,
I have a question regarding Parameters
of the object Query
in the JPA
.
I am implementing pagination of my tables using the Primefaces
with LazyDataModel
and in the function I use to make the pagination I do 2 searches in the database. The first to filter the data and return only those of the precise range q. And the second to return the COUNT
of the total records for the filter (no 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]
. Effectively returning only the results I want from the base.
Therefore, I would like to be able to reuse the filter parameters of the first Query
, in the second.
Something like that:
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’ve tried to do something like this. But the second one Query
does not execute with an error saying that the expected parameter was not found.
I look forward to your contributions, thank you.
Just a question about the performance of your application: would it not be more efficient to read the size of the
List
that was returned in the first query?– Weslley Tavares
It would really be more 'efficient', but it is not the value I desire. See, the first Query returns only the values for table pagination (at max 10 per page), so I would get a size of 10 for my table at all times, breaking the pagination. .
– Israel Merljak