How is it possible to apply the pagination to the below query:
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select b from Building b where b.id in :ids" )
Page<Post> findByIds(@Param("ids") List<Long> postIdsList);
...
}
All the existing examples are based on the standard findAll method that accepts a Pageable object: public Page findAll(Pageable pageable);.
The questions are:
- what the controller method signature should be
- what the repository method parameters should be
- how and what parameters should be passed into the controller method
- should I always split the post IDs for every request
- will Spring make a single query and keep all the found posts in memory or it will hit a query every time for every next/previous page? If so, how can it figure out the IDs to use to find the next/previous posts?
The initial implementation was as follows:
@RestController
class PostsController {
@Autowired
private PostService postService;
@GetMapping("/posts", params = "ids")
public List<Post> getPaginatedPosts(@RequestParam List<Long> ids) {
return postService.findPaginatedPosts(ids);
}
}
@Repository
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select b from Building b where b.id in :ids" )
Page<Post> findByIds(@Param("ids") List<Long> postIdsList);
...
}
I omitted the code from the PostServiceImpl qui implements the PostService and just calls the PostRepository#findByIds method.