如何使用findByParentCode从OneToMany单向父级获取分页子级列表 [英] How to get paginated child lists from OneToMany unidrectional Parent using findByParentCode

查看:195
本文介绍了如何使用findByParentCode从OneToMany单向父级获取分页子级列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单向单亲父母-孩子.
我正在使用父级获取子级列表.
如何在子记录列表上应用分页?
例如:父母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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆