簡體   English   中英

播放框架-使用JPA分頁

[英]Play Framework - Pagination using JPA

似乎以下語法無效。 我們對此有其他查詢嗎?

JPA.em().createQuery(queryStr).getResultList().from(startAt).fetch(offset);

眾所周知,from()和fetch()僅適用於JPAQueryobject,並且上述代碼將產生List而不是JPAQueryobject。

請注意,queryStr組合了2種不同的模型。

無論如何,要從上述查詢中獲取JPAQueryobject? 這樣我就可以使用from和fetch了。

您能否更精確地說明“沒有用”的部分? 您有任何錯誤或類似錯誤嗎?

在我的應用程序上,我實現了一些分頁功能,而JPA查詢的示例就是這個( News是我的應用程序的一種模型):

public static void news(int size, int page) {
    // 'size' is the number of elements displayed per page
    // 'page' is the current page index, starting from 1.
    int start = page * size;
    List<News> allNews = News.find("order by date desc").from(start).fetch(size);
    // Once the list of news is found, we return them in Json format...
    renderJSON(allNews, new NewsJsonSerializer());
}

關於您的編輯:方法createQuery返回一個Query對象。 因此,可以使用setFirstResult()方法和setMaxResults()代替from()fetch() 應用到您的代碼后,查詢現在看起來像:

JPA.em().createQuery(queryStr).setFirstResult(startAt).setMaxResults(offset).getResultList();

暫無
暫無

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

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