How can I do paging with @OneToMany collections

后端 未结 2 1063
清酒与你
清酒与你 2021-02-19 01:53

Suppose I have a Post entity and a Comment entity and a one to many relationship:

@Entity class Post {
    ...
    @OneToMany
    List comments;
          


        
2条回答
  •  深忆病人
    2021-02-19 02:41

    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);
    

提交回复
热议问题