Spring Data JPA Query Methods

Query methods refer to the methods or functions used to retrieve data from a database or a data source. These methods allow you to specify the criteria for data retrieval and return the relevant data that matches those criteria.

Spring Data JPA Query Methods allow you to define queries in Spring Data repositories by simply declaring method signatures without writing explicit SQL queries. Spring Data JPA automatically generates the necessary query based on the method name, method parameters, and entity relationships. These methods are powerful and easy to use for basic CRUD operations and custom search queries.

Here's an example of a Spring Data JPA Query Method:

List<User> findByFirstName(String firstName);

In this example, the method retrieves a list of User entities whose firstName matches the provided parameter.

Here are some common keywords used in Spring Data JPA Query Methods, along with examples:

KeywordExampleDescription
AndList<User> findByFirstNameAndLastName(String firstName, String lastName)
Joins multiple conditions using logical AND.
OrUser findByIdOrEmail(String id, String email)
Joins multiple conditions using logical OR.
After, IsAfter
List<User> findByCreateDateAfter(Date date)
After and IsAfter keywords are used for date-based queries. Both keywords can be used interchangeably to achieve the same date-based query result. It is used to query data whose eventDate is after the provided date.
Before, isBefore
List<User> findByCreateDateBefore(Date date)
Before and IsBefore keywords are used for date-based queries. Both keywords can be used interchangeably to achieve the same date-based query result. It is used to query data whose eventDate is before the provided date.
Between, IsBetween
List<Payment> findByPaymentDateBetween(Date startDate, Date endDate)
It is used to query data whose eventDate is between the provided startDate and endDate.
Containing, IsContaining, Contains
List<User> findByFirstNameContaining(String partialName)
Containing, IsContaining, and Contains keywords are used for partial string matching.
EndingWith, IsEndingWith, EndsWith
List<User> findByFirstNameEndingWith(String suffix)
EndingWith, IsEndingWith, and EndsWith keywords are used for performing queries based on the end of a string.
exists
boolean existsByEmail(String email)
Exists keyword is used to check for the existence of entities that match a certain condition.
False, IsFalse
List<Order> findByPaidIsFalse()
False and IsFalse keywords are used to perform queries based on boolean values.
GreaterThan, IsGreaterThan
List<Product> findByPriceGreaterThan(double price)
GreaterThan and IsGreaterThan keywords are used to perform queries based on comparison with numeric or comparable properties. The example method retrieves a list of Product entities whose price is greater than the provided price.
GreaterThanEqual, IsGreaterThanEqual
List<Product> findByPriceGreaterThanEqual(double price)
GreaterThanEqual and IsGreaterThanEqual keywords are used to perform queries based on comparison with numeric or comparable properties. The example method retrieves a list of Product entities whose price is greater than or equal to the provided value.
In, IsIn
List<User> findByIdIn(List<String> ids)
In and IsIn keywords are used to perform queries based on a property matching a list of values. The example query will return a list of User entities whose IDs match the values in the list.
Is, Equals
List<User> findByFirstNameEquals(String firstName)
 Is and Equals keywords are used to perform queries based on exact matches.
IgnoreCase
List<User> findByFirstNameIgnoreCase(String firstName)
 IgnoreCase keyword is used to perform case-insensitive queries.
Empty, IsEmpty
List<User> findByLastNameEmpty()
Empty and IsEmpty keywords are used to perform queries based on checking for empty collections or strings.
NotEmpty, IsNotEmpty
List<User> findByLastNameNotEmpty()NotEmpty and IsNotEmpty keywords are used to perform queries based on checking for non empty collections or strings.
NotNull, IsNotNull
List<User> findByLastNameNotNull()
NotNull and IsNotNull keywords are used to perform queries based on checking for non-null values.
Null, IsNull
List<User> findByLastNameIsNull()
Null and IsNull keywords are used to perform queries based on checking for null values.
LessThan, IsLessThan
List<Product> findByPriceLessThan(double price)
LessThan and IsLessThan keywords are used to perform queries based on comparison with numeric or comparable properties. The example method retrieves a list of Product entities whose price is less than the provided value.
LessThanEqual, IsLessThanEqual
List<Product> findByPriceLessThanEqual(double price)
The example method retrieves a list of Product entities whose price is less than or equal to the provided value.
Like, IsLike
List<User> findByFirstNameLike(String pattern);
Like and IsLike keywords are used to perform queries based on pattern matching with strings.
Near, IsNear
List<Place> findByLocationNear(GeoJsonPoint location, Distance maxDistance);
 Near or IsNear keywords are used for geospatial queries like those provided by some NoSQL databases or specialized geospatial libraries. In this example, Place is an entity class with a location property that represents the geospatial coordinates as a GeoJsonPoint. findByLocationNear allows you to search for places near a specific location within a maximum distance defined by maxDistance. It's important to note that these geospatial features are specific to Spring Data MongoDB or other geospatial extensions. Standard Spring Data JPA does not have built-in support for geospatial queries, and you may need to use native SQL or extend your data access to a database with geospatial capabilities.
Not, IsNot
List<User> findByLastNameNot(String lastName)
Not and IsNot keywords are used to perform queries with negation.
NotIn, IsNotIn
List<User> findByEmailNotIn(List<String> emails)NotIn and IsNotIn keywords are used to perform queries based on properties not matching any of the values in a provided list.
NotLike, IsNotLike
List<User> findByFirstNameNotLike(String pattern)
NotLike or IsNotLike keywords are used for performing negated LIKE queries with pattern matching.
OrderBy
List<Transaction> findByStatusOrderByCreateDateDesc(String status)
List<Transaction> findByStatusOrderByCreateDateAsc(String status)
OrderBy keyword is used to sort query results based on a specific property in ascending or descending order. By default, query results are sorted in ascending order when using the OrderBy keyword.
Regex, MatchesRegex, Matches
@Query(value = "SELECT * FROM student WHERE first_name :pattern", nativeQuery = true)
public List<User> findByFirstNameRegex(String pattern)
Spring Data JPA does not provide built-in support for regular expression (regex) queries in query methods. Standard Spring Data JPA Query Methods support basic comparison operations but do not directly support complex regular expression queries. However, you can still achieve regex-based queries using custom queries with the @Query annotation. This allows you to write native SQL queries that utilize regular expressions for pattern matching.
StartingWith, IsStartingWith, StartsWith
List<User> findByFirstNameStartingWith(String prefix)
StartingWith, IsStartingWith, and StartsWith keywords are used to perform queries based on the start characters matching of a string.
True, IsTrue
List<Order> findByPaidIsTrue()
True and IsTrue keywords are used to perform queries based on boolean values.