I have a repository, that returns a Page<Mind>
:
public interface MindRepository extends PagingAndSortingRepository<Mind, Integer> {
Page<Mind> findByCountry(String country, Pageable pageable);
}
And a controller that use it:
private MindRepository mindRepository;
@GetMapping(path = "/minds", produces = "application/json")
public Page<Mind> getMinds(String country, Integer page, Integer size) {
Pageable pageable = PageRequest.of(page,size);
return mindRepository.findByCountry(country,pageable);
}
And everything is ok. Controller returns Page<Mind>
in json that suits FrontEnd.
But now I have to make the query more complicated, with several filters, changing dynamically. I would like to use createQuery
like this:
public interface CustomizedMindRepository<T> {
Page<T> findByCountry(String country, Pageable pageable);
}
public interface MindRepository extends PagingAndSortingRepository<Mind, Integer>,CustomizedMindRepository {
Page<Mind> findByCountry(String country, Pageable pageable);
}
public class CustomizedMindRepositoryImpl implements CustomizedMindRepository {
@PersistenceContext
private EntityManager em;
@Override
public Page<Mind> findByCountry(String country, Pageable pageable) {
return em.createQuery("from minds where <dynamical filter> AND <another dynamical filter> AND <...etc>", Mind.class)
.getResultList();
}
}
But getResultList()
returns List
, not Page
:(
What is the best way to solve it?