One of the most powerful features of Micronaut Data is its ability to generate database queries at compile-time just by analyzing method names. This approach is called Finder Methods or Derived Queries.
π You don’t need to write SQL for common queries.
π You don’t need to write any custom implementation.
π Micronaut will do the heavy lifting for you β safely and efficiently.

π What are Finder Methods?
Finder Methods are special method names you define inside your repository interface.
Micronaut parses these names and automatically generates SQL queries for them at compile-time.
For example:
1 2 3 |
List<Student> findByName(String name); |
π Micronaut will automatically generate:
1 2 3 4 |
SELECT * FROM students WHERE name = ? |
β How Finder Methods Work
Micronaut Data uses naming conventions to determine:
- Which field(s) to query.
- What operation to apply (
equals
,greater than
,like
, etc). - Whether it’s a single record or list.
- Whether sorting or pagination is needed.
π« Sample Entity: Student
Letβs continue using the same entity from our previous post – Defining Entities and Repositories in Micronaut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.micronaut.school.entities; import io.micronaut.data.annotation.*; import java.time.LocalDate; @MappedEntity("students") public class Student { @Id @GeneratedValue(GeneratedValue.Type.IDENTITY) private Long id; private String name; private LocalDate dateOfBirth; private String email; // Getters and Setters } |
π§ Creating Finder Methods
1οΈβ£ Find by Name
1 2 3 |
List<Student> findByName(String name); |
Generated Query:
1 2 3 4 |
SELECT * FROM students WHERE name = ? |
2οΈβ£ Find by Email
1 2 3 |
Optional<Student> findByEmail(String email); |
Generated Query:
1 2 3 |
SELECT * FROM students WHERE email = ? |
Use Optional<T>
for single results (helps avoid nulls).
3οΈβ£ Find by Name and Email
1 2 3 |
Optional<Student> findByNameAndEmail(String name, String email); |
Generated Query :
1 2 3 4 |
SELECT * FROM students WHERE name = ? AND email = ? |
4οΈβ£ Find Students Born After a Date
1 2 3 |
List<Student> findByDateOfBirthAfter(LocalDate date); |
Generated Query:
1 2 3 4 |
SELECT * FROM students WHERE date_of_birth > ? |
β
Supports comparison operators: After
, Before
, GreaterThan
, LessThan
, etc.
5οΈβ£ Case Insensitive Search
1 2 3 |
List<Student> findByNameIlike(String name); |
Ilike
does a case-insensitive search.
Generated Query:
1 2 3 4 |
SELECT * FROM students WHERE LOWER(name) LIKE LOWER(?) |
6οΈβ£ Partial Match with Like
1 2 3 |
List<Student> findByNameLike(String namePattern); |
Pass %pattern%
to match substrings.
1 2 3 4 |
studentRepository.findByNameLike("%John%"); |
Generated Query:
1 2 |
SELECT * FROM students WHERE name LIKE ? |
π₯ Using Sorting with Finder Methods
Micronaut allows sorting results automatically.
Sort by Name Ascending
1 2 3 |
List<Student> findByEmailOrderByNameAsc(String email); |
Generated Query:
1 2 |
SELECT * FROM students WHERE email = ? ORDER BY name ASC |
π Full Repository Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kscodes.micronaut.school.repositories; import com.kscodes.micronaut.school.entities.Student; import io.micronaut.data.annotation.Repository; import io.micronaut.data.repository.CrudRepository; import java.time.LocalDate; import java.util.List; import java.util.Optional; @Repository public interface StudentRepository extends CrudRepository<Student, Long> { List<Student> findByName(String name); Optional<Student> findByEmail(String email); Optional<Student> findByNameAndEmail(String name, String email); List<Student> findByDateOfBirthAfter(LocalDate date); List<Student> findByNameLike(String namePattern); List<Student> findByNameIlike(String name); List<Student> findByEmailOrderByNameAsc(String email); } |
π Advantages of Finder Methods
β
No boilerplate code
β
Compile-time validation
β
Type-safe queries
β
No runtime query errors
β
Faster development
π Common Errors
Error | Cause | Solution |
---|---|---|
No property found | Invalid method name | Match exact field names |
Cannot resolve repository | Bean not found | Check package scanning |
Query returns wrong data | Method name logic incorrect | Review field names and operators |
π Supported Keywords
Here are some of the keywords Micronaut Data supports in Finder Methods:
Keyword | Description |
---|---|
And / Or | Combine conditions |
Between | Range search |
LessThan , GreaterThan | Comparisons |
Like / Ilike | Wildcard matching |
IsNull / IsNotNull | Null checks |
In | Search in a list |
OrderBy | Sorting results |
β Full list available in Micronaut Data Docs – Query Methods
β Summary
- We learned how to write Finder Methods in Micronaut Data.
- Finder Methods reduce boilerplate and increase productivity.
- Queries are validated at compile time.
- You can easily build complex queries without SQL.