This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Relational 3.4.0!spring-doc.cn

Query Methods

Most of the data access operations you usually trigger on a repository result in a query being run against the databases. Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:spring-doc.cn

Example 1. PersonRepository with query methods
interface ReactivePersonRepository extends ReactiveSortingRepository<Person, Long> {

  Flux<Person> findByFirstname(String firstname);                                   (1)

  Flux<Person> findByFirstname(Publisher<String> firstname);                        (2)

  Flux<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable); (3)

  Mono<Person> findByFirstnameAndLastname(String firstname, String lastname);       (4)

  Mono<Person> findFirstByLastname(String lastname);                                (5)

  @Query("SELECT * FROM person WHERE lastname = :lastname")
  Flux<Person> findByLastname(String lastname);                                     (6)

  @Query("SELECT firstname, lastname FROM person WHERE lastname = $1")
  Mono<Person> findFirstByLastname(String lastname);                                (7)
}
1 The method shows a query for all people with the given firstname. The query is derived by parsing the method name for constraints that can be concatenated with And and Or. Thus, the method name results in a query expression of SELECT … FROM person WHERE firstname = :firstname.
2 The method shows a query for all people with the given firstname once the firstname is emitted by the given Publisher.
3 Use Pageable to pass offset and sorting parameters to the database.
4 Find a single entity for the given criteria. It completes with IncorrectResultSizeDataAccessException on non-unique results.
5 Unless <4>, the first entity is always emitted even if the query yields more result rows.
6 The findByLastname method shows a query for all people with the given last name.
7 A query for a single Person entity projecting only firstname and lastname columns. The annotated query uses native bind markers, which are Postgres bind markers in this example.

Note that the columns of a select statement used in a @Query annotation must match the names generated by the NamingStrategy for the respective property. If a select statement does not include a matching column, that property is not set. If that property is required by the persistence constructor, either null or (for primitive types) the default value is provided.spring-doc.cn

The following table shows the keywords that are supported for query methods:spring-doc.cn

Table 1. Supported keywords for query methods
Keyword Sample Logical result

Afterspring-doc.cn

findByBirthdateAfter(Date date)spring-doc.cn

birthdate > datespring-doc.cn

GreaterThanspring-doc.cn

findByAgeGreaterThan(int age)spring-doc.cn

age > agespring-doc.cn

GreaterThanEqualspring-doc.cn

findByAgeGreaterThanEqual(int age)spring-doc.cn

age >= agespring-doc.cn

Beforespring-doc.cn

findByBirthdateBefore(Date date)spring-doc.cn

birthdate < datespring-doc.cn

LessThanspring-doc.cn

findByAgeLessThan(int age)spring-doc.cn

age < agespring-doc.cn

LessThanEqualspring-doc.cn

findByAgeLessThanEqual(int age)spring-doc.cn

age <= agespring-doc.cn

Betweenspring-doc.cn

findByAgeBetween(int from, int to)spring-doc.cn

age BETWEEN from AND tospring-doc.cn

NotBetweenspring-doc.cn

findByAgeNotBetween(int from, int to)spring-doc.cn

age NOT BETWEEN from AND tospring-doc.cn

Inspring-doc.cn

findByAgeIn(Collection<Integer> ages)spring-doc.cn

age IN (age1, age2, ageN)spring-doc.cn

NotInspring-doc.cn

findByAgeNotIn(Collection ages)spring-doc.cn

age NOT IN (age1, age2, ageN)spring-doc.cn

IsNotNull, NotNullspring-doc.cn

findByFirstnameNotNull()spring-doc.cn

firstname IS NOT NULLspring-doc.cn

IsNull, Nullspring-doc.cn

findByFirstnameNull()spring-doc.cn

firstname IS NULLspring-doc.cn

Like, StartingWith, EndingWithspring-doc.cn

findByFirstnameLike(String name)spring-doc.cn

firstname LIKE namespring-doc.cn

NotLike, IsNotLikespring-doc.cn

findByFirstnameNotLike(String name)spring-doc.cn

firstname NOT LIKE namespring-doc.cn

Containing on Stringspring-doc.cn

findByFirstnameContaining(String name)spring-doc.cn

firstname LIKE '%' + name +'%'spring-doc.cn

NotContaining on Stringspring-doc.cn

findByFirstnameNotContaining(String name)spring-doc.cn

firstname NOT LIKE '%' + name +'%'spring-doc.cn

(No keyword)spring-doc.cn

findByFirstname(String name)spring-doc.cn

firstname = namespring-doc.cn

Notspring-doc.cn

findByFirstnameNot(String name)spring-doc.cn

firstname != namespring-doc.cn

IsTrue, Truespring-doc.cn

findByActiveIsTrue()spring-doc.cn

active IS TRUEspring-doc.cn

IsFalse, Falsespring-doc.cn

findByActiveIsFalse()spring-doc.cn

active IS FALSEspring-doc.cn

Modifying Queries

The previous sections describe how to declare queries to access a given entity or collection of entities. Using keywords from the preceding table can be used in conjunction with delete…By or remove…By to create derived queries that delete matching rows.spring-doc.cn

Example 2. Delete…By Query
interface ReactivePersonRepository extends ReactiveSortingRepository<Person, String> {

  Mono<Integer> deleteByLastname(String lastname);            (1)

  Mono<Void> deletePersonByLastname(String lastname);         (2)

  Mono<Boolean> deletePersonByLastname(String lastname);      (3)
}
1 Using a return type of Mono<Integer> returns the number of affected rows.
2 Using Void just reports whether the rows were successfully deleted without emitting a result value.
3 Using Boolean reports whether at least one row was removed.

As this approach is feasible for comprehensive custom functionality, you can modify queries that only need parameter binding by annotating the query method with @Modifying, as shown in the following example:spring-doc.cn

@Modifying
@Query("UPDATE person SET firstname = :firstname where lastname = :lastname")
Mono<Integer> setFixedFirstnameFor(String firstname, String lastname);

The result of a modifying query can be:spring-doc.cn

  • Void (or Kotlin Unit) to discard update count and await completion.spring-doc.cn

  • Integer or another numeric type emitting the affected rows count.spring-doc.cn

  • Boolean to emit whether at least one row was updated.spring-doc.cn

The @Modifying annotation is only relevant in combination with the @Query annotation. Derived custom methods do not require this annotation.spring-doc.cn

Modifying queries are executed directly against the database. No events or callbacks get called. Therefore also fields with auditing annotations do not get updated if they don’t get updated in the annotated query.spring-doc.cn

Alternatively, you can add custom modifying behavior by using the facilities described in Custom Implementations for Spring Data Repositories.spring-doc.cn

Using @Query

The following example shows how to use @Query to declare a query method:spring-doc.cn

Declare a query method by using @Query
interface UserRepository extends ReactiveCrudRepository<User, Long> {

  @Query("select firstName, lastName from User u where u.emailAddress = :email")
  Flux<User> findByEmailAddress(@Param("email") String email);
}
Note that String-based queries do not support pagination nor accept Sort, PageRequest, and Limit as a query parameter as for these queries the query would be required to be rewritten. If you want to apply limiting, please express this intent using SQL and bind the appropriate parameters to the query yourself.
Spring fully supports Java 8’s parameter name discovery based on the -parameters compiler flag. By using this flag in your build as an alternative to debug information, you can omit the @Param annotation for named parameters.

Queries with SpEL Expressions

Query string definitions can be used together with SpEL expressions to create dynamic queries at runtime. SpEL expressions can provide predicate values which are evaluated right before running the query.spring-doc.cn

Expressions expose method arguments through an array that contains all the arguments. The following query uses [0] to declare the predicate value for lastname (which is equivalent to the :lastname parameter binding):spring-doc.cn

@Query("SELECT * FROM person WHERE lastname = :#{[0]}")
Flux<Person> findByQueryWithExpression(String lastname);

SpEL in query strings can be a powerful way to enhance queries. However, they can also accept a broad range of unwanted arguments. You should make sure to sanitize strings before passing them to the query to avoid unwanted changes to your query.spring-doc.cn

Expression support is extensible through the Query SPI: org.springframework.data.spel.spi.EvaluationContextExtension. The Query SPI can contribute properties and functions and can customize the root object. Extensions are retrieved from the application context at the time of SpEL evaluation when the query is built.spring-doc.cn

When using SpEL expressions in combination with plain parameters, use named parameter notation instead of native bind markers to ensure a proper binding order.