Hi Im learning Spring MVC and Spring Data JPA and am trying to create a new Page using a List of objects retrieved from the database. First I get all the elements from the DB, convert it to a Stream and then use lambda to filter the results. Then I need a Page with a set number of elements, however, instantiating a new PageImpl doesnt seem to return a page with the correct size.
Here is my code:
Stream<Produtos> stream = produtosRepository.findAll().stream();
listaFinal = stream.filter(p -> p.getProdNome().contains("uio")).collect(Collectors.toList());
long total = listaFinal.size();
Page<Produtos> imp = new PageImpl<>(listaFinal,pageable,total);
extends JpaRepository<Produtos, Integer>, QueryDslPredicateExecutor<Produtos>
p -> p.getProdNome().contains("uio")
After learning more about how Spring Data works I ended up using
@Query annotations on my methods inside the JpaRepository implementations to properly query the DB and filter the results, eliminating the need to use a stream and then convert back to Page.
Here's how the code above would look in case anyone needs an example:
@Query("select p from Produtos p where p.prodNome = ?1") public Page<Produtos> productsListByName(String prodNome, Pageable pageable)
Im aware of Spring's
findBy methods but sometimes the method names become really difficult to read depending on the amount of parameters so I just stuck to JPQL.
Doing it this way the Page's content will always have up to the maximum amount of elements defined by you in the Spring configuration.
I also use a custom implementation of
PageImpl, I'm not at work right now and don't have access to the code, but I'll post it whenever I can.
Edit: Custom implementation can be found here.