In the previous article, we learned the basics of Spring Data JPA and created a simple Employee Management module using JpaRepository. One of the biggest advantages of Spring Data JPA is that developers can create database queries simply by writing method names.
This feature is called Derived Query Methods.
Instead of writing SQL queries manually, Spring Data JPA automatically generates queries based on repository method names. This reduces development time and keeps the code cleaner and more maintainable.
In this article, we will extend our Employee Management System and learn how to search for employees using different conditions.
What are Derived Query Methods?
Derived Query Methods are repository methods where Spring automatically generates SQL queries from the method name itself.
Example:
findByDepartment(String department)
Spring internally converts this into:
SELECT*FROM employeesWHERE department= ?
No SQL query is required.
Real-World Scenario
Suppose your HR Management System requires the following features:
Find employees by department
Search for employees whose salary is greater than a specific amount
Find employees by name
Search employees whose names contain specific keywords
These operations can be implemented easily using Derived Query Methods.
Update Repository Interface
Modify the EmployeeRepository.
packagecom.ayshriv.repository;
importcom.ayshriv.entity.Employee;
importorg.springframework.data.jpa.repository.JpaRepository;
importjava.util.List;
publicinterfaceEmployeeRepositoryextendsJpaRepository<Employee,Long> {
List<Employee>findByDepartment(Stringdepartment);
List<Employee>findBySalaryGreaterThan(Doublesalary);
List<Employee>findByNameContaining(Stringkeyword);
EmployeefindByName(Stringname);
}
How Method Naming Works
Spring Data JPA understands keywords inside method names.
Example 1
findByDepartment(String department)
Generated Query:
SELECT*FROM employeesWHERE department= ?
Example 2
findBySalaryGreaterThan(Double salary)
Generated Query:
SELECT*FROM employeesWHERE salary> ?
Example 3
findByNameContaining(String keyword)
Generated Query:
SELECT*FROM employeesWHERE nameLIKE%keyword%
Update Service Layer
packagecom.ayshriv.service;
importcom.ayshriv.entity.Employee;
importcom.ayshriv.repository.EmployeeRepository;
importorg.springframework.stereotype.Service;
importjava.util.List;
@Service
publicclassEmployeeService {
privatefinalEmployeeRepositoryemployeeRepository;
publicEmployeeService(EmployeeRepositoryemployeeRepository) {
this.employeeRepository=employeeRepository;
}
publicList<Employee>getEmployeesByDepartment(Stringdepartment) {
returnemployeeRepository.findByDepartment(department);
}
publicList<Employee>getEmployeesBySalary(Doublesalary) {
returnemployeeRepository.findBySalaryGreaterThan(salary);
}
publicList<Employee>searchEmployees(Stringkeyword) {
returnemployeeRepository.findByNameContaining(keyword);
}
}
Update Controller
packagecom.ayshriv.controller;
importcom.ayshriv.entity.Employee;
importcom.ayshriv.service.EmployeeService;
importorg.springframework.web.bind.annotation.*;
importjava.util.List;
@RestController
@RequestMapping("/employees")
publicclassEmployeeController {
privatefinalEmployeeServiceemployeeService;
publicEmployeeController(EmployeeServiceemployeeService) {
this.employeeService=employeeService;
}
@GetMapping("/department/{department}")
publicList<Employee>getByDepartment(@PathVariableStringdepartment) {
returnemployeeService.getEmployeesByDepartment(department);
}
@GetMapping("/salary/{salary}")
publicList<Employee>getBySalary(@PathVariableDoublesalary) {
returnemployeeService.getEmployeesBySalary(salary);
}
@GetMapping("/search/{keyword}")
publicList<Employee>searchEmployees(@PathVariableStringkeyword) {
returnemployeeService.searchEmployees(keyword);
}
}
API Testing Examples
Find Employees by Department
GET /employees/department/IT
Find Employees with Salary Greater Than 50000
GET /employees/salary/50000
Search Employees by Name
GET /employees/search/rahul
Common Derived Query Keywords
Keyword | Example |
|---|---|
And | findByNameAndDepartment |
Or | findByNameOrDepartment |
GreaterThan | findBySalaryGreaterThan |
LessThan | findBySalaryLessThan |
Containing | findByNameContaining |
StartingWith | findByNameStartingWith |
EndingWith | findByNameEndingWith |
OrderBy | findBySalaryOrderByNameAsc |
Advantages of Derived Query Methods
1. Faster Development
No need to write SQL queries manually.
2. Cleaner Repository Layer
Method names clearly describe functionality.
3. Better Readability
Other developers can easily understand the logic.
4. Reduced Boilerplate Code
Spring automatically generates implementations.
Conclusion
Derived Query Methods are one of the most powerful features of Spring Data JPA. By simply following naming conventions, developers can generate complex queries without writing SQL manually.
In this article, we implemented:
Department-based search
Salary filtering
Name search functionality
Dynamic query generation using method names
These features are commonly used in production-level backend applications.
In the next article, we will learn:
Custom JPQL and Native SQL Queries using @Query Annotation
We will explore how to write custom database queries when Derived Query Methods are not enough.



