簡體   English   中英

當第一個實體是具有指定主鍵的實體之后的后續實體時,如何使用 JPA 獲取列表

[英]How to getList using JPA when the first entity is the following entity after the entity with specified primary key

要獲取帶有limit列表offset ,我可以使用:

@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();
        }

現在,我有某個實體的主鍵,而不是偏移量。

如何在具有@ID 的元素之后的元素之后獲取List?

示例:我的數據庫中的實體具有鍵 - a, b, c, d, e, f

我需要執行getList(b, 3) 實體的結果列表將具有鍵 - c, d, e

這行不通。 對於當前數據,您可以與rank()一起select實體,找到指定 id 的rank()值,然后使用它來查詢以下數據(此函數是特定於供應商的,例如在 postgres、sql server 中可用)。 但是您沒有使用order by ,是什么導致未定義結果排序。 在數據庫中保存一些數據后,順序可能會發生變化,結果也不會相同。

如果有可能你可以使用id ,那么你可以這樣寫:

"select collection from " + Collection.class.getName() + " collection where collection.id > :id order by collection.id";

並傳遞一個參數id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM