Syntax
limit – result of the query can be limited by usage of first/top keyword
findFirst10ByLastname
findFirstByOrderByLastnameAsc
findTop3ByLastname
findTopByOrderByAgeDesc
property/properties expression – result will be filtered based on property of entity, multiple properties can be used with usage of And, Or keyword
findByLastnameAndFirstname
findByLastnameOrFirstname
findByFirstname
comparison – comparison mode can be specified after specifying property used for filtering
findByFirstnameIs
findByFirstnameEquals
findByStartDateBetween
findByAgeLessThan
,findByAgeLessThanEqual
findByAgeGreaterThan
,findByAgeGreaterThanEqual
findByStartDateBefore
,findByStartDateAfter
findByAgeIsNull
,findByAgeIsNotNull
findByFirstnameLike
,findByFirstnameNotLike
findByFirstnameStartingWith
,findByFirstnameEndingWith
findByFirstnameContaining
findByLastnameNot
findByAgeIn(Collection ages)
,findByAgeNotIn(Collection ages)
findByActiveTrue
,findByActiveFalse
findByFirstnameIgnoreCase
ordering operator – optionally you can specify ordering operator at the end of method name
findByLastnameOrderByFirstnameAsc
findByLastnameOrderByFirstnameDesc
Spring doc:
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html
Supported query method subject keywords
The following table lists the subject keywords generally supported by the Spring Data repository query derivation mechanism to express the predicate. Consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
Keyword | Description |
---|---|
find…By read…By get…By query…By search…By stream…By | General query method returning typically the repository type, a Collection or Streamable subtype or a result wrapper such as Page , GeoResults or any other store-specific result wrapper. Can be used as findBy… , findMyDomainTypeBy… or in combination with additional keywords. |
exists…By | Exists projection, returning typically a boolean result. |
count…By | Count projection returning a numeric result. |
delete…By , remove…By | Delete query method returning either no result (void ) or the delete count. |
…First<number>… , …Top<number>… | Limit the query results to the first <number> of results. This keyword can occur in any place of the subject between find (and the other keywords) and by . |
…Distinct… | Use a distinct query to return only unique results. Consult the store-specific documentation whether that feature is supported. This keyword can occur in any place of the subject between find (and the other keywords) and by . |
Supported query method predicate keywords and modifiers
The following table lists the predicate keywords generally supported by the Spring Data repository query derivation mechanism. However, consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
Logical keyword | Keyword expressions |
---|---|
AND | And |
OR | Or |
AFTER | After , IsAfter |
BEFORE | Before , IsBefore |
CONTAINING | Containing , IsContaining , Contains |
BETWEEN | Between , IsBetween |
ENDING_WITH | EndingWith , IsEndingWith , EndsWith |
EXISTS | Exists |
FALSE | False , IsFalse |
GREATER_THAN | GreaterThan , IsGreaterThan |
GREATER_THAN_EQUALS | GreaterThanEqual , IsGreaterThanEqual |
IN | In , IsIn |
IS | Is , Equals , (or no keyword) |
IS_EMPTY | IsEmpty , Empty |
IS_NOT_EMPTY | IsNotEmpty , NotEmpty |
IS_NOT_NULL | NotNull , IsNotNull |
IS_NULL | Null , IsNull |
LESS_THAN | LessThan , IsLessThan |
LESS_THAN_EQUAL | LessThanEqual , IsLessThanEqual |
LIKE | Like , IsLike |
NEAR | Near , IsNear |
NOT | Not , IsNot |
NOT_IN | NotIn , IsNotIn |
NOT_LIKE | NotLike , IsNotLike |
REGEX | Regex , MatchesRegex , Matches |
STARTING_WITH | StartingWith , IsStartingWith , StartsWith |
TRUE | True , IsTrue |
WITHIN | Within , IsWithin |
In addition to filter predicates, the following list of modifiers is supported:
Keyword | Description |
---|---|
IgnoreCase , IgnoringCase | Used with a predicate keyword for case-insensitive comparison. |
AllIgnoreCase , AllIgnoringCase | Ignore case for all suitable properties. Used somewhere in the query method predicate. |
OrderBy… | Specify a static sorting order followed by the property path and direction (e. g. OrderByFirstnameAscLastnameDesc ). |
https://docs.spring.io/spring-data/jpa/reference/repositories/query-keywords-reference.html
Keyword | Sample | JPQL snippet |
---|---|---|
Distinct | findDistinctByLastnameAndFirstname | select distinct … where x.lastname = ?1 and x.firstname = ?2 |
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is , Equals | findByFirstname ,findByFirstnameIs ,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull , Null | findByAge(Is)Null | … where x.age is null |
IsNotNull , NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended % ) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended % ) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in % ) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstname) = UPPER(?1) |