How to request data from a box using <JPY> JPA data both by sorting and by page?

I am trying Spring JPA data in my project. I want to know if there is a ready-made API for querying data using both Sort and Pageable . Of course, I know that I can write this method myself, I just want to know if there is one ready. My DAO extends JpaRepository , and I found that there are the following methods that I can call:

 findAll(); findAll(Pageable pageable); findAll(Sort sort); 

But there is no method like findAll(Sort sort, Pageable pageable) , so I'm interested.

+61
java spring spring-data-jpa jpa
May 10 '12 at 3:26
source share
2 answers

There are two ways to achieve this:

 final PageRequest page1 = new PageRequest( 0, 20, Direction.ASC, "lastName", "salary" ); final PageRequest page2 = new PageRequest( 0, 20, new Sort( new Order(Direction.ASC, "lastName"), new Order(Direction.DESC, "salary") ) ); dao.findAll(page1); 

As you can see, the second form is more flexible, as it allows you to define a different direction for each property ( lastName ASC, salary DESC ).

+132
May 10 '12 at 6:19 06:19
source share

Pageable also has the ability to specify sorting. From java doc

 PageRequest(int page, int size, Sort.Direction direction, String... properties) 

Creates a new PageRequest parameter with the applicable collation.

+10
May 10 '12 at 5:09
source share



All Articles