D
D
DDwrt1002019-06-04 17:52:47
Java
DDwrt100, 2019-06-04 17:52:47

How to get the number of responses after a query is executed?

Good afternoon.
There is such a request with pagination.

List<Pages> datarecord1 = Dao.getSession()
                    .createQuery("FROM Page as d  WHERE d.dateTimeCreated BETWEEN  :startTimeDate AND :endTimeDate ORDER By d.dateTimeCreated", Pages.class)
                    .setParameter("startTimeDate", startTimeDate)
                    .setParameter("endTimeDate", endTimeDate)
                    .setFirstResult(startResult)
                    .setMaxResults(onPage)
                    .stream()
                    .map(restConverterService::Pages)
                    .collect(Collectors.toList());

The code gives a certain number of records, then converts it into a response class. However, there is a need to also give a number that shows the total number of responses received if there was no pagination.
Who can tell how to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-04
@DDwrt100

Receive as a separate request

long totalPages = (Long) Dao.getSession()
     .createQuery("select count(*) from Page")
     .uniqueResult();

But since it is a countrelatively heavy function, it is better to cache the result, and reset the cache when the number of lines changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question