我想获取一个实体的最后 5 条记录。但无法通过 Spring Data JPA 获取。
最初我试图通过 LIMIT 查询获取数据,但 JPA 不支持 LIMIT。
后来我尝试了 Pageable
接口(interface)。
Pageable pageCount = new PageRequest(0, 10, Direction.ASC,"id");
List<TxnEntity> txnEntities = txnDAO
.findByAccountEntity(accountEntity,pageCount);
这给了我 10 个对象的第一页。
但我的要求是获得最后 10 或 5 个对象。那么如何通过Spring框架中的Pageable
接口(interface)获取呢?
最佳答案
来自引用文档:
To find out how many pages you get for a query entirely you have to trigger an additional count query. By default this query will be derived from the query you actually trigger.
您可以通过提前发出计数查询来确定页数:
Long count = txnDAO.countByAccountEntity(accountEntity);
Pageable pageCount = new PageRequest((int)count/10, 10, Direction.ASC,"id");
关于java - Spring Data JPA,如何通过Pageable获取最后一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326361/