Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA, how to get the last Page through Pageable

I wanted to fetch the last 5 records of an entity. But unable to fetch it via Spring Data JPA.

Initially I was trying to get the data via LIMIT query but LIMIT isn't supported in JPA.

Later I tried with Pageable interface.

Pageable pageCount = new PageRequest(0, 10, Direction.ASC,"id");
List<TxnEntity> txnEntities = txnDAO
            .findByAccountEntity(accountEntity,pageCount);

This gives me first page with 10 Objects.

But my requirement is to get last 10 or 5 Objects. So how can I get it via Pageable interface in the Spring framework?

like image 206
virsha Avatar asked May 19 '15 12:05

virsha


1 Answers

You could reverse your sort and then get the first 5.

like image 169
objectuser Avatar answered Sep 20 '22 12:09

objectuser