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
Tom May 10 '12 at 3:26 2012-05-10 03:26
source share2 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
Tomasz Nurkiewicz May 10 '12 at 6:19 2012-05-10 06:19
source sharePageable 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
gkamal May 10 '12 at 5:09 a.m. 2012-05-10 05:09
source share