How to use CrudRepository with variations of parameters?

Viewed 2169

I want to show a table of persons. The user should be able to send queries and filter by attributes that are mostly optional.

Problem: for each attribute to filter, I'd have to introduce a additional method in spring-data-jpa using `CrudRepository:

public interface PersonRepository extends CrudRepository<Person, Long> {
    List<Person> findByFirstname(firstname);
    List<Person> findbyFirstnameAndLastname(first, last);
    List<Person> findByFirstnameAndLastnameAndAge(first, last, age);
    List<Person> findByFirstnameAndLastnameAndAgeAndCity(first, last, age, city);
}

Question: how could I do better (without having to write native PreparedStatements myself)?

3 Answers
Related