问题描述
要使用limit
与offset
的getList形式,我可以使用:
To getList form offset
with limit
I can use:
@Override
public List<Collection> getList(int offset, int limit) {
String SELECT_COLLECTIONS = "select collection from " + Collection.class.getName() + " collection";
TypedQuery<Collection> query = entityManager.createQuery(SELECT_COLLECTIONS, Collection.class);
return query.setFirstResult(offset).setMaxResults(limit).getResultList();
}
现在,我有了某个实体的主键,而不是偏移量.
Now instead of offset i have primary key of some entity.
如何在具有@ID的元素之后的元素之后的元素之后获取List?
示例:我数据库中的实体具有键- a,b,c,d,e,f .
Example: the entites in my db have keys - a, b, c, d, e, f.
我需要执行 getList(b,3).实体的结果列表将具有键- c,d,e .
I need to execute getList(b, 3). The result list of entites would have the keys - c, d, e.
推荐答案
这不起作用.对于当前数据,您可以将select
实体与rank()
一起,找到指定ID的rank()
值,然后使用它查询以下数据(此功能是特定于供应商的,例如在postgres,sql server中可用) .但是,您没有使用order by
,这导致未定义结果排序.将一些数据保存到数据库中后,顺序可能会更改,结果将有所不同.
This won't work. For current data you could select
entities together with rank()
, find the value of rank()
for specified id and then use it to query the following data (this function is vendor specific, available for example in postgres, sql server). But you're not using order by
, what makes the result ordering not defined. After you save some data in your database, the ordering could change and the result won't be the same.
如果有可能可以使用id
,则可以编写如下内容:
If there is a possibility you can use id
, then you can write something like this:
"select collection from " + Collection.class.getName() + " collection where collection.id > :id order by collection.id";
并传递参数id
.
这篇关于当第一个实体是具有指定主键的实体之后的下一个实体时,如何使用JPA获取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!