0

I am trying to implement pagination and sorting in spring MVC. As per my understanding, we can use PagingAndSortingRepository or JpaRepository for same(http://springinpractice.com/2012/05/11/pagination-and-sorting-with-spring-data-jpa).

But both these use the default findAll method to do so.

I wish to create my own method and execute a custom query and perform the pagination as well as sorting on that(lets say search by category name and sort by creation date). I am not sure how to do this by using PagingAndSortingRepository or JpaRepository.

It will be great if I can have some sort of guidance to achieve this.

Thanks in advance.

2
  • I don't see a question here.... Commented Aug 25, 2015 at 2:30
  • @ChiefTwoPencils I have updated/edited the question Commented Aug 25, 2015 at 2:38

1 Answer 1

1

With JPA you can do many combination of queries just by specifying method signatures. Please consult http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

In your repository interface you can

List<Person> findAll(); // this is standard List<Person> findById(String id); // looking person that have specific Id List<Person> findByNameLike(String name); // you can put the name "foo%" 

If you want pagination and sorting...

Page<Person> findByNameLike(String name, PageRequest pageRequest); 

And you use it like

int page = 0; // first page int size = 10; // show 10 result max per page Page personPage = repo.findByNameLike("A%", new PageRequest(page,size,Sort.Direction.ASC, "birthDate")); // pagination for person with name, page 0, 10 item per page, and sorted by the person.birthDate. 

Good luck

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. Can you please share detailed tutorial for same?
You can see this documentation docs.spring.io/spring-data/data-jpa/docs/1.0.0.M1/reference/… Comes from this stackoverflow.com/questions/14120153/… Its pretty straight forward

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.