Spring Data JPA,如何获取最后一页通过Pageable [英] Spring Data JPA, how to get the last Page through Pageable

查看:821
本文介绍了Spring Data JPA,如何获取最后一页通过Pageable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取实体的最后5条记录。但无法通过Spring Data JPA获取它。



最初,我试图通过LIMIT查询获取数据,但在JPA中不支持LIMIT。 b
$ b

后来我尝试使用 Pageable 界面。

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

这给了我第一页10个对象。



但我的要求是获得最后10或5个对象。那么如何通过Spring框架中的 Pageable 接口获得它?

p>我期待的输出是最后n条记录。

因此我使用了Spring Data JPA 1.7内置查询

  int n = 10; 

Pageable pageCount = new PageRequest(0,n);

列出< TxnEntity> txnEntities = txnDAO
.findByAccountEntityOrderByIdDesc(accountEntity,pageCount);

现在这将返回最后n条记录。



DAO将以降序返回所有记录,并且通过使用Pageable,我们可以从底部获取多少记录。



很简单。



更多JPA查询内置查询: http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

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?

解决方案

The output I was expecting was last n records.

So I have used the Spring Data JPA 1.7 inbuilt query

int n = 10;

Pageable pageCount = new PageRequest(0, n);

List<TxnEntity> txnEntities = txnDAO
        .findByAccountEntityOrderByIdDesc(accountEntity,pageCount);

Now this will return last n records.

The DAO will return all records in descending order, and by using Pageable we can how many records we need from bottom.

It was simple.

For more JPA query inbuilt queries : http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

这篇关于Spring Data JPA,如何获取最后一页通过Pageable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆