Question

We can do a List<MyInfo> findAll(List<Integer> ids) in spring data. How do i combine this with pagination?

On the lines of:

findAll(ids, pageRequest);

I tried something on the lines of creating a specification and using that, but i'm doing something wrong:

public class MySpecs {

    public static Specification<MyInfo> idsIn(final List<Integer> ids)
    {

        return new Specification<MyInfo>() {

            @Override
            public Predicate toPredicate(Root<MyInfo> root,
                    CriteriaQuery<?> query, CriteriaBuilder cb) {
                return root.get("id").in(ids);


            }

        };
    }
}


myRepo.findAll(MySpecs.isIn(ids),pageRequest)

Thanks

Was it helpful?

Solution

The easiest way is to simply declare a query method like this:

interface MyInfoRepository extends Repository<MyInfo, Integer> {

  Page<MyInfo> findByIdIn(List<Integer> ids, Pageable pageable);
}

OTHER TIPS

This was never a problem. In fact, what I have mentioned above is correct and works.

My result set had just 1 row, and i was checking results from page 1 sized 5. Pagination, like everything else, starts from 0.

If anyone reads this, this is not a problem. If you encounter the same problem, please verify the index of the page you are requesting/testing with.

Good night.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top