JPA処理条件inのページングクエリ


第1種
時間:2018-11-13 17:17:47提出
Pageable pageable = PageRequest.of(pageQueryDto.getPage() - 1, pageQueryDto.getSize(), Sort.Direction.fromString(pageQueryDto.getDirection()), pageQueryDto.getProperty());
Page page = projectRepository.findAll((root, query, cb) -> {
    List list = new ArrayList();
    CriteriaBuilder.In in = cb.in(root.get("f").get("id"));
    for (F f : pageQueryDto.getList()) {
        in.value(f.getId());
    }
    list.add(in);
    Predicate[] p = new Predicate[list.size()];
    return cb.and(list.toArray(p));
}, pageable);

==2019年4月25日10:36:46更新
第2種
Pageable pageable = PageRequest.of(pageQueryDto.getPage() - 1, pageQueryDto.getSize(), Sort.Direction.fromString(pageQueryDto.getDirection()), pageQueryDto.getProperty());
Page page = projectRepository.findAll((root, query, cb) -> {
    Predicate predicate = root.isNotNull();
    predicate = cb.and(predicate, cb.and(root.get("id").as(Long.class).in(pageQueryDto.getList())));
    return predicate;
}, pageable);

第3種
// pageQueryDto            
Pageable pageable = PageRequest.of(pageQueryDto.getPage() - 1, pageQueryDto.getSize(), Sort.Direction.fromString(pageQueryDto.getDirection()), pageQueryDto.getProperty());

Page page = projectRepository.findAll((root, query, cb) -> {
    Predicate predicate = root.isNotNull();
    CriteriaBuilder.In in = cb.in(root.get("id").as(Long.class));
	for (Long id : pageQueryDto.getList()) {
		in.value(id);
	}
	predicate = cb.and(predicate, in);
    return predicate;
}, pageable);

2つ目の方法をお勧めします