简体   繁体   English

播放框架-使用JPA分页

[英]Play Framework - Pagination using JPA

It seems to be the following syntax didn't work. 似乎以下语法无效。 Do we have any alternate query for this? 我们对此有其他查询吗?

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

As we know, from() and fetch() will work only on JPAQueryobject and the above code will produce List instead of JPAQueryobject. 众所周知,from()和fetch()仅适用于JPAQueryobject,并且上述代码将产生List而不是JPAQueryobject。

Please note that queryStr combines 2 different models. 请注意,queryStr组合了2种不同的模型。

Is there anyway to get JPAQueryobject from the above query? 无论如何,要从上述查询中获取JPAQueryobject? So that I can use from and fetch. 这样我就可以使用from和fetch了。

Could you be a little more precise about the "didn't work" part? 您能否更精确地说明“没有用”的部分? Do you have any error, or something like that? 您有任何错误或类似错误吗?

On my application, I implemented some pagination features, and an example of the JPA query is this one ( News is one model of my application): 在我的应用程序上,我实现了一些分页功能,而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());
}

Regarding your edit: The method createQuery returns a Query object. 关于您的编辑:方法createQuery返回一个Query对象。 Thus, you can use setFirstResult() method and setMaxResults() instead of from() and fetch() . 因此,可以使用setFirstResult()方法和setMaxResults()代替from()fetch() Applied on your code, your query now looks like: 应用到您的代码后,查询现在看起来像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM