Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Pagination using entity manager in spring

How can I implement pagination in Spring + hibernate project ? Following is the code. I will get PageRequest object and I want to return Page of item

 @Repository
    public class ItemRepository {

      @PersistenceContext
      EntityManager entityManager;

      public Page<Item> findItems(PageRequest pageRequest) {
        // TODO: Implement me
        return new Page<>(new ArrayList<>(), 0, 0);
      }
    }


    public class PageRequest {

      private final int count;
      private final int pageNumber;

      public PageRequest(int pageNumber, int count) {
        assert pageNumber >= 0;
        assert count > 0;

        this.pageNumber = pageNumber;
        this.count = count;
      }

      public int getCount() {
        return count;
      }

      public int getPageNumber() {
        return pageNumber;
      }
    }
like image 755
Sonali Avatar asked Jan 25 '23 06:01

Sonali


2 Answers

I found the solution

public Page<Item> findItems(PageRequest pageRequest) {

    Query query = entityManager.createQuery("From Item");
    int pageNumber =pageRequest.getPageNumber();
    int pageSize = pageRequest.getCount();
    query.setFirstResult((pageNumber) * pageSize);
    query.setMaxResults(pageSize);
    List <Item> fooList = query.getResultList();

    Query queryTotal = entityManager.createQuery
            ("Select count(f.id) From Item f");
    long countResult = (long)queryTotal.getSingleResult();
    int i=(int)countResult;
    return new Page<>(fooList, pageRequest.getPageNumber(),i);
  }

I had to query two times to DB once to get records and then to get all the count

like image 199
Sonali Avatar answered Jan 29 '23 12:01

Sonali


One way to do it is to add logic in your PageRequest class to "slice" an incoming list depending on its Pageable method parameter and return it as org.springframework.data.domain.PageImpl.

Here is a static method that you can use in your PageRequest class:

public static <E> Page<E> returnPagedList(Pageable pageable, List<E> listOfEntities) {
    List<E> listToReturn = listOfEntities;
    if (pageable.isPaged()) {
        int pageSize = pageable.getPageSize();
        int currentPage = pageable.getPageNumber();
        int startItem = currentPage * pageSize;
        if (listOfEntities.size() < startItem) {
            listToReturn = Collections.emptyList();
        } else {
            int toIndex = Math.min(startItem + pageSize, listOfEntities.size());
            listToReturn = listOfEntities.subList(startItem, toIndex);
        }
    }
    return new PageImpl<>(listToReturn, pageable, listOfEntities.size());
}

Then in your repository you can do this (notice you receive a Pageable object):

  public Page<Item> findItems(Pageable pageable) {
   EntityManager em = getEntityManager();
   List<Item> list = ... // get list of all Items
   return PageRequest.pagedList(pageable, list);
    }
  }
like image 44
Petar Bivolarski Avatar answered Jan 29 '23 11:01

Petar Bivolarski