Search code examples
javaspringjpaspring-data-jpapageable

JPA findAll Pageable Request Total Count Group By Column


I have a code like below:

Pageable pageableRequest = PageRequest.of(page, limit, Sort.by(sortBy));
Page<User> userList= null;
userList = this.user.findAll(pageableRequest)

from here I get all the data and total is shown. However, I want to group the total by status. How can I achieve that? Now total is 26 and I have 3 status inactive, active and vip.


Solution

  • If you want to have all users grouped by status you can do something like below.

    Ask for all users and than group it in java

    List<User> userList = this.user.findAll();
    Map<Status, List<User>> mapResult = userList.stream()
        .collect(Collectors.groupingBy(User::getStatus));