你能对“没有工作”部分更精确一点吗?你有任何错误,或类似的东西?
在我的申请,我实现了一些分页功能,和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();
非常感谢您的回答。我编辑了这个问题,现在更清楚了。 –
@Siva我编辑了我的帖子,为您提供了另一个代码示例,这更接近您的建议。我认为这是你正在寻找的... – romaintaz