Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

JPA Methods

Posted on June 12, 2024June 13, 2024 by Armando Marques

Table of Contents

Toggle
  • Syntax
  • Supported query method subject keywords
  • Supported query method predicate keywords and modifiers

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.

KeywordDescription
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…ByExists projection, returning typically a boolean result.
count…ByCount projection returning a numeric result.
delete…By, remove…ByDelete 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 keywordKeyword expressions
ANDAnd
OROr
AFTERAfter, IsAfter
BEFOREBefore, IsBefore
CONTAININGContaining, IsContaining, Contains
BETWEENBetween, IsBetween
ENDING_WITHEndingWith, IsEndingWith, EndsWith
EXISTSExists
FALSEFalse, IsFalse
GREATER_THANGreaterThan, IsGreaterThan
GREATER_THAN_EQUALSGreaterThanEqual, IsGreaterThanEqual
INIn, IsIn
ISIs, Equals, (or no keyword)
IS_EMPTYIsEmpty, Empty
IS_NOT_EMPTYIsNotEmpty, NotEmpty
IS_NOT_NULLNotNull, IsNotNull
IS_NULLNull, IsNull
LESS_THANLessThan, IsLessThan
LESS_THAN_EQUALLessThanEqual, IsLessThanEqual
LIKELike, IsLike
NEARNear, IsNear
NOTNot, IsNot
NOT_INNotIn, IsNotIn
NOT_LIKENotLike, IsNotLike
REGEXRegex, MatchesRegex, Matches
STARTING_WITHStartingWith, IsStartingWith, StartsWith
TRUETrue, IsTrue
WITHINWithin, IsWithin

In addition to filter predicates, the following list of modifiers is supported:

KeywordDescription
IgnoreCase, IgnoringCaseUsed with a predicate keyword for case-insensitive comparison.
AllIgnoreCase, AllIgnoringCaseIgnore 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

KeywordSampleJPQL snippet
DistinctfindDistinctByLastnameAndFirstnameselect distinct …​ where x.lastname = ?1 and x.firstname = ?2
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is, EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNull, NullfindByAge(Is)Null… where x.age is null
IsNotNull, NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstname) = UPPER(?1)

  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb