How can I do paging with @OneToMany collections

白昼怎懂夜的黑 提交于 2019-12-03 23:04:19

Is it possible to emulate dynamic paging with @OneToMany collections on top of JPA (...)

Not supported. The standard approach would be to use a JPQL query to retrieve the comments for a given post and and to use Query#setFirstResult(int) and Query#setMaxResults(int).

On my first thought, we could create a subclass of List, (...). But I don't know whether Hibernate/JPA will recognize this, or interfere with it.

It obviously won't without an heavy patch to drastically change the default behavior.

I think the "right" way might be more like:

@Entity
class Post {
    ...

    public GenericModel.JPAQuery getComments() {
        return Comment.find("post_id = ?", post_id);
    }
}

and then use one of the fetch methods in JPAQuery:

// fetch first page of results, 25 results per page
post.getComments().fetch(1,25);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!