1

I have a list of Spring Repository methods which has grown more and more as users request additional filters. i.e., these are five example methods out of the 20 I currently have:

public interface ManagerRepository extends Repository<Manager, Long> {
    List<Manager> findManagerByName(Long id);
    List<Manager> findManagerByNameAndProjects(Long id, String project)
    List<Manager> findManagerByNameAndPojectLike(Long id, String project)
    List<Manager> findManagerByNameAndPojectStartsWith(Long id, String project)
    List<Manager> findManagerByNameAndProjectLikeAndEmployeesLike(Long id, String project, String EmployeeName
}

The # of filters is seemingly endless as I get new requirements, and I don't think I'm utilizing Spring Repositories correctly in this fashion. How can I handle additional filters without creating new methods (As the current methods might be duplicated if we want to create another search for something crazy like:

findManagerByNameStartsWithAscAndProjectsLikeIgnoreCaseAndMonthStarted
(String Name, String project, String Month)
Clay Banks
  • 4,483
  • 15
  • 71
  • 143
  • 2
    Use specifications instead of numerous query methods. See http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications. The query methods are meant for the simply cases for the more complex stuff use specifications. Also http://stackoverflow.com/questions/27626825/spring-data-jpa-query-by-example – M. Deinum Oct 15 '15 at 05:41
  • Also this answer is a good read http://stackoverflow.com/questions/20280708/filtering-database-rows-with-spring-data-jpa-and-spring-mvc/20283256#20283256 (disclaimer it was mine also :) ). – M. Deinum Oct 15 '15 at 05:50
  • As M. Deinum said, take a look at specifications, however also take a look at QueryDSL: http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/ – Krešimir Nesek Oct 15 '15 at 07:30
  • Very helpful, thank you both – Clay Banks Oct 16 '15 at 00:39

0 Answers0