0

I want to only retrieve 100 results from my Query using the Spring data CrudRepository?

For example my current query is a derieved query: findAll()

How can I alter this to only return 100 results and to order them by a certain column?

java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

2

Say, your crud repo is:

public interface CustomerRepository 
           extends PagingAndSortingRepository<Customer, Integer> {   

}

You can retrieve first 100 items using the following query:

Page<Customer> items = customerRepo.findAll(
    new PageRequest(1, 100, 
                new Sort(new Sort.Order(Sort.Direction.ASC, "id"))));

The code above means that you retrieve first page with 100 items, sorted by the bean property named "id".

Artem Larin
  • 142
  • 6