Derived Query Methods
About
Derived Query Methods are a powerful feature of Spring Data JPA that allow developers to define queries by simply declaring method names in repository interfaces. Spring Data JPA parses the method name and automatically generates the appropriate JPQL or SQL query behind the scenes.
This means:
No need to write JPQL or native SQL
Method name = Query definition
Characteristics
Convention over Configuration
We follow naming conventions instead of writing queries.
Auto-generated Queries
Spring parses method names and creates queries automatically.
Readable & Declarative
Method names are self-explanatory and easy to understand.
Less Boilerplate Code
No need for custom query implementations.
Integration with Entities
Works seamlessly with JPA entity models.
Customizable with Keywords
Support for And
, Or
, Between
, Like
, In
, OrderBy
, etc.
How It Works ?
When Spring Data JPA boots up:
It scans repository interfaces.
If a method name starts with a known keyword (e.g.,
findBy
,deleteBy
,countBy
, etc.), it will:Parse the rest of the method name.
Derive a JPQL query based on it.
Create a dynamic proxy implementation to execute it.
Example:
Spring will interpret this as:
Invalid Method Names or Ambiguities
Spring fails with
IllegalStateException
if method name is ambiguous or unparseable.We can fix this by:
Using
@Query
Refactoring the name
Simplifying complex logic into custom methods
Supported Query Keywords
findBy
Fetch data
readBy
Same as findBy
getBy
Same as findBy
queryBy
Same as findBy
countBy
Count records
existsBy
Boolean existence check
deleteBy
Deletes matching records
removeBy
Same as deleteBy
Common Keywords After By
By
And
AND
Or
OR
Between
BETWEEN
LessThan
<
LessThanEqual
<=
GreaterThan
>
GreaterThanEqual
>=
IsNull
IS NULL
IsNotNull
IS NOT NULL
Like
LIKE
NotLike
NOT LIKE
In
IN
NotIn
NOT IN
StartingWith
LIKE 'abc%'
EndingWith
LIKE '%abc'
Containing
/ Contains
LIKE '%abc%'
True/False
= true / false
IgnoreCase
case-insensitive match
Examples
Prerequisites
Assume the following Employee
entity:
And a repository interface:
Basic Queries
Find employees in a specific department.
Find employee by exact name.
Combined Conditions
Find employees in a department with salary above a threshold.
Find employees either by name or department.
Comparison Operators
Filter employees based on salary or join date.
Pattern Matching
Partial match on name field.
Sorting & Limiting
Top N queries and sorting.
Date Ranges & Collection Checks
Filter employees by date range or multiple departments.
Boolean Checks
Check if a record exists or count employees in a department.
Null / Not Null Checks
Case-Insensitive Queries
Limiting Results
Comparison: Derived Query vs JPQL vs Native SQL
Ease of Use
✅ Easiest
⚠️ Moderate
❌ Requires SQL knowledge
Custom Logic
❌ Limited
✅ Moderate
✅ Full control
Portability
✅ High
✅ High
❌ Database-specific
Type-Safety
✅ Yes
❌ No
❌ No
Complex Queries
❌ Hard
⚠️ Manageable
✅ Best option
Last updated
Was this helpful?