Query Repository Method Naming

Spring Data JPA provides a powerful method-naming convention for query generation. By following these conventions, developers can create readable and efficient queries without writing explicit SQL or JPQL. This article explores various query method naming strategies, including equality, similarity, comparison conditions, multiple condition expressions, sorting, and recent changes in CrudRepository.

1. Equality Condition Keywords

Exact equality is a common condition in queries. We have several options to express = or IS operators:

  • Append the property name without a keyword for an exact match:

    1
    
    List<User> findByName(String name);
  • Use Is or Equals for readability:

    1
    2
    
    List<User> findByNameIs(String name);
    List<User> findByNameEquals(String name);
  • Express inequality with IsNot:

    1
    
    List<User> findByNameIsNot(String name);

Spring Data JPA automatically handles null parameters as IS NULL. We can explicitly use IsNull or IsNotNull:

1
2
List<User> findByNameIsNull();
List<User> findByNameIsNotNull();

For boolean fields, True and False keywords add equality conditions:

1
2
List<User> findByActiveTrue();
List<User> findByActiveFalse();

2. Similarity Condition Keywords

For pattern-based queries:

  • Match values starting with a prefix:

    1
    
    List<User> findByNameStartingWith(String prefix);
  • Match values ending with a suffix:

    1
    
    List<User> findByNameEndingWith(String suffix);
  • Match values containing a substring:

    1
    
    List<User> findByNameContaining(String infix);

For custom patterns, use Like:

1
List<User> findByNameLike(String likePattern);

Example usage:

1
2
String likePattern = "a%b%c";
userRepository.findByNameLike(likePattern);

3. Comparison Condition Keywords

For numeric comparisons:

1
2
3
4
List<User> findByAgeLessThan(Integer age);
List<User> findByAgeLessThanEqual(Integer age);
List<User> findByAgeGreaterThan(Integer age);
List<User> findByAgeGreaterThanEqual(Integer age);

To find users between two ages:

1
List<User> findByAgeBetween(Integer startAge, Integer endAge);

For collection-based queries:

1
List<User> findByAgeIn(Collection<Integer> ages);

For date comparisons:

1
2
List<User> findByBirthDateAfter(ZonedDateTime birthDate);
List<User> findByBirthDateBefore(ZonedDateTime birthDate);

4. Multiple Condition Expressions

We can combine conditions using And and Or:

1
2
List<User> findByNameOrBirthDate(String name, ZonedDateTime birthDate);
List<User> findByNameOrBirthDateAndActive(String name, ZonedDateTime birthDate, Boolean active);

And takes precedence over Or, following Java’s operator precedence.

For complex queries, consider using the @Query annotation.

5. Sorting the Results

Sorting can be applied with OrderBy:

1
2
List<User> findByNameOrderByName(String name);
List<User> findByNameOrderByNameAsc(String name);

For descending order:

1
List<User> findByNameOrderByNameDesc(String name);

6. findOne vs. findById in CrudRepository

Spring Boot 2.x changed findOne to findById:

1
User user = userRepository.findById(1);

The findById() method is already defined in CrudRepository, eliminating the need for custom implementations.


By following these naming conventions, we can write concise, readable, and efficient queries in Spring Data JPA.

0%