1

I use Spring Data JPA for my repository layer. I need to query entities by every possible property and combinations of its properties. So my typical repository looks like this.

public interface UserRepository extends JpaRepository<User, Long> {

    User findByLogin(String login);
    User findByEmail(String email);
    User findByName(String name);
    User findByLoginAndEmail(String login, String email);
    User findByLoginAndName(String login, String name);
    User findByLoginAndEmailAndName(String login, String email, String name);
    //and so on
}

Even with three properties it looks ugly enough. Are there any more flexible alternatives to this approach? Or this design is OK(at least I do not have to implements all of these methods)?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
  • You can try something from here https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ – Orest Mar 18 '17 at 09:59
  • Possible duplicate of [Spring Data dynamic query](http://stackoverflow.com/questions/23017419/spring-data-dynamic-query) – ltsallas Mar 18 '17 at 11:15

1 Answers1

1

Found query by example. Seems it is what I was looking for.

Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62