Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA apply sorting, pagination along with a where clause

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';
like image 902
Pushkar Avatar asked Jan 02 '13 10:01

Pushkar


People also ask

How can I apply pagination in JPA?

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.

How do you implement pagination and sorting?

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.

Is Spring data provide support for pagination and sorting?

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.


Video Answer


2 Answers

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.

like image 192
burna Avatar answered Sep 18 '22 12:09

burna


The easiest way I could find is to use QueryDSL or Specifications. I prefer QueryDSL.

like image 38
Hari Menon Avatar answered Sep 19 '22 12:09

Hari Menon