I am currently Using Spring JPA and utilizing the Sorting and Pagination as described here -
How to query data via Spring data JPA by sort and pageable both out of box?
Now my question is how do I apply a 'where' clause to this?
E.g.
where PRODUCT_NAME='abc';
The simplest way to implement pagination is to use the Java Query Language – create a query and configure it via setMaxResults and setFirstResult: Query query = entityManager. createQuery("From Foo"); int pageNumber = 1; int pageSize = 10; query.
Once we have our repository extending from PagingAndSortingRepository, we just need to: Create or obtain a PageRequest object, which is an implementation of the Pageable interface. Pass the PageRequest object as an argument to the repository method we intend to use.
Spring Data provides support for pagination. It creates all the logic to implement paging, like a count for the rows for all the pages and more.
Just create a query method in your repository interface for this.
Take a look in the docs, here.
Add a query method with paging and where clause.
Page<User> findByLastname(String lastname, Pageable pageable);
So you can find User by a property "lastname" and configure Paging settings.
The easiest way I could find is to use QueryDSL or Specifications. I prefer QueryDSL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With