如何使用findByParentCode从OneToMany单向父级获取分页子级列表 [英] How to get paginated child lists from OneToMany unidrectional Parent using findByParentCode
问题描述
我有一个单向单亲父母-孩子.
我正在使用父级获取子级列表.
如何在子记录列表上应用分页?
例如:父母P有孩子C1,C2,C3..C20
我的确findByParentCode(Code)返回带有子列表的Parent对象.但是我需要获取孩子的分页清单.
请帮助
I have onetomany unidrirectional parent - child.
I am fetching the child list using parent.
How do i apply pagination on the list of child records?
For Ex: Parent P has Child C1,C2,C3..C20
i do findByParentCode(Code) returns the Parent object with Child list in it. But i need to get the paginated list of the child.
Please Help
推荐答案
您可以使用子对象中的父代码字段来查询子对象的表,即,父表的外键类似于:
You can query your child object's table using the parent code field in the child object i.e the foreign key to the Parent table something like:
Page<Child> findByParentId(Long parentId, Pageable pageRequest);
您可以使用Spring Data JPA分页支持对查询结果进行分页.创建 PageRequest 类,其中包含pageNumber,大小和排序(如果需要),并如下所示遍历页面
You can paginate the query results by using Spring Data JPA pagination support. Create an instance of PageRequest class with the pageNumber, size and sorting if need be, and iterate over the pages as shown below
Page<Child> page = null;
do {
Pageable nextPageable = page != null ? page.nextPageable() : new PageRequest(0, pageSize);
page = childService.findByParentId(parentId, nextPageable);
processPage(page);
} while (page.hasNext());
这篇关于如何使用findByParentCode从OneToMany单向父级获取分页子级列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!