How about using a query like select count(distinct u.name) from UserEntity u join u.orders o where u.createdAt > ‘2018-01-01’
to count the elements?
I would suggest you take a look at Blaze-Persistence Pagination API which will handle this automatically for you. You just write a query like
PagedList<Tuple> pagedList = criteriaBuilderFactory.create(entityManager, Tuple.class)
.select("u.name")
.select("sum(u.orders.amount)")
.from(User.class, "u")
.where("u.createdAt").gtExpression("DATE '2018-01-01'")
.page(firstResult, maxResults)
.getResultList();
pagedList.getTotalSize(); // The total count
pagedList.size(); // The size of the page