1

I searched everywhere for an example of a Spring piece of code using simultaneously these 3 JPA concepts, very important when querying :

  • filtering - using Example, ExampleMatcher

  • paging - using Pageable (or similar)

  • sorting - using Sort

So far I only saw examples using only 2 of them at the same time but I need to use all of them at once. Can you show me such an example?

Thank you.

PS: This has examples for Paging and Sorting but not filtering.

Yann39
  • 14,285
  • 11
  • 56
  • 84
MS13
  • 327
  • 5
  • 16
  • Assuming you have not succeeded in adding the 3rd technique to an example already using the other 2: What problem did you encounter, when you tried? – andrewJames Dec 20 '20 at 19:30
  • I didn't find a way, that's why I am asking – MS13 Dec 20 '20 at 20:11
  • I want to filter (with many fields from the entity) and do paging and sort in a web page and I cannot find a way to do that with Spring JPA. Found examples on sorting or filtering or paging but not them all together – MS13 Dec 20 '20 at 21:47
  • Yes, you state that clearly in your question. I was hoping that you could provide some specific technical details for whatever specific problem you are facing (for example, a [mre]). – andrewJames Dec 20 '20 at 21:50
  • I will delete the question if there are ppl who are offended by it, but to me this is a site of good practices not a site where u debug other ppls code. – MS13 Dec 20 '20 at 23:06
  • 1
    If you have found a solution (one which is different from the excellent answer by Yann39) then that is great, and you are always welcome to post it in its own answer. (I can't see any reason why anyone would be offended by your question. I see it was down-voted - perhaps because it does not have much in the way of specific details). – andrewJames Dec 20 '20 at 23:09
  • I had other questions that also werent much specific but they didn't get downvoted. Maybe today there was some stubborn user who just feel frustrated by it for some unknown personal reason. I mean what minimal reproducible example has this question? https://stackoverflow.com/questions/6025998/difference-between-view-and-request-scope-in-managed-beans – MS13 Dec 21 '20 at 09:34

2 Answers2

2

Here is an example, searching for news on title attribute, with pagination and sorting :

Entity :

@Getter
@Setter
@Entity
public class News {

    @Id
    private Long id;

    @Column
    private String title;

    @Column
    private String content;

}

Repository :

public interface NewsRepository extends JpaRepository<News, Long> {

}

Service

@Service
public class NewsService {

    @Autowired
    private NewsRepository newsRepository;

    public Iterable<News> getNewsFilteredPaginated(String text, int pageNumber, int pageSize, String sortBy, String sortDirection) {

        final News news = new News();
        news.setTitle(text);

        final ExampleMatcher matcher = ExampleMatcher.matching()
                .withIgnoreCase()
                .withIgnorePaths("content")
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);

        return newsRepository.findAll(Example.of(news, matcher), PageRequest.of(pageNumber, pageSize, sortDirection.equalsIgnoreCase("asc") ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending()));

    }
}

Call example :

for (News news : newsService.getNewsFilteredPaginated("hello", 0, 10, "title", "asc")) {
    log.info(news.getTitle());
}
Yann39
  • 14,285
  • 11
  • 56
  • 84
2

Found the answer in the end after more research:

public Page<MyEntity> findAll(MyEntity entityFilter, int pageSize, int currentPage){
    ExampleMatcher matcher = ExampleMatcher.matchingAll()
        .withMatcher("name", exact()); //add filters for other columns here
    Example<MyEntity> filter = Example.of(entityFilter, matcher); 
    Sort sort = Sort.by(Sort.Direction.ASC, "id"); //add other sort columns here
    Pageable pageable = PageRequest.of(currentPage, pageSize, sort); 
    return repository.findAll(filter, pageable);
}
MS13
  • 327
  • 5
  • 16