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.
Please note that queryStr combines 2 different models.
Is there anyway to get JPAQueryobject from the above query? So that I can use from and 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):
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. Thus, you can use setFirstResult()
method and setMaxResults()
instead of from()
and fetch()
. Applied on your code, your query now looks like:
JPA.em().createQuery(queryStr).setFirstResult(startAt).setMaxResults(offset).getResultList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.