0

I'm trying to get the total count of my query but doesn't work.

The query is big so I'll show to final

TypedQuery<Order> query = em.createQuery(criteriaQuery);

        query.setFirstResult(Math.toIntExact(pageable.getOffset()));
        query.setMaxResults(pageable.getPageSize());

        Predicate[] predicateArray = new Predicate[p.size()];
        p.toArray(predicateArray);

        return new PageImpl<>(query.getResultList(), pageable, getTotalCount(criteriaBuilder, predicateArray));

and I create a function to make this count

private Long getTotalCount(CriteriaBuilder criteriaBuilder, Predicate[] predicateArray){
        CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
        Root<Order> from = criteriaQuery.from(Order.class);

        criteriaQuery.select(criteriaBuilder.count(from));
        criteriaQuery.where(predicateArray);

        return em.createQuery(criteriaQuery).getSingleResult();
    }
  • Does it answer your problem? https://stackoverflow.com/a/2884378/1439560 – Huy Nguyen Oct 25 '21 at 14:05
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 25 '21 at 15:25

0 Answers0