Discover how naming a method right can save you hours of tedious SQL writing!
Why Custom query methods by naming convention in Spring Boot? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine writing SQL queries by hand every time you want to fetch data from a database in your Spring Boot app.
You have to write long, complex queries for simple tasks like finding users by name or sorting products by price.
Manually writing queries is slow and error-prone.
It's easy to make typos or forget to handle special cases.
Also, mixing SQL strings inside your Java code makes it messy and hard to maintain.
Spring Boot lets you create query methods just by naming them clearly.
The framework understands the method name and builds the query automatically.
This means less code, fewer mistakes, and cleaner, easier-to-read repositories.
List<User> findByName(String name); // then write SQL manually
List<User> findByName(String name); // Spring Data JPA builds query from method nameYou can quickly create powerful database queries just by writing simple method names, speeding up development and reducing bugs.
Imagine an online store where you want to find all products under $50 sorted by popularity.
Instead of writing SQL, you just add a method named findByPriceLessThanOrderByPopularityDesc.
Manual SQL writing is slow and error-prone.
Custom query methods use clear names to auto-generate queries.
This makes code cleaner, faster to write, and easier to maintain.
Practice
findByLastName do?Solution
Step 1: Understand the method prefix
The prefixfindByin Spring Data JPA means it will search and return matching records.Step 2: Analyze the property name
The method usesLastNameas the property to filter by, so it finds records with that lastName.Final Answer:
Finds all records where the lastName matches the given value -> Option DQuick Check:
findBy + property = find matching records [OK]
- Confusing findBy with deleteBy or countBy
- Thinking it returns a boolean instead of records
- Ignoring the property name after findBy
Solution
Step 1: Identify the correct prefix for counting
The correct prefix to count records iscountBy.Step 2: Check method naming pattern
The method should becountByAgeto count users filtered by age.Final Answer:
countByAge(int age); -> Option CQuick Check:
countBy + property = count matching records [OK]
- Adding extra words like Users in method name
- Using findCountBy which is invalid
- Placing property name after count instead of after By
existsByEmailAndStatus(String email, String status), what will it return if a user with email "test@example.com" and status "active" exists?Solution
Step 1: Understand the method prefix
The prefixexistsByreturns a boolean indicating if any record matches the criteria.Step 2: Analyze the condition
The method checks if a user exists with the given email and status combined withAnd.Final Answer:
true -> Option BQuick Check:
existsBy + conditions returns boolean [OK]
- Expecting a list instead of boolean
- Confusing existsBy with findBy
- Ignoring the combined conditions with And
List<User> findByNameOr(int age, String name);Solution
Step 1: Analyze the method name structure
The method name uses 'Or' but does not specify the property after 'Or'. It should be like 'findByNameOrAge'.Step 2: Check parameter order and names
Parameters should match the properties in the method name order, but the main error is missing property after 'Or'.Final Answer:
The method name is missing the property after 'Or' -> Option AQuick Check:
Method names must specify property after 'Or' [OK]
- Leaving out property name after 'Or' or 'And'
- Mixing parameter order with method name order
- Using wrong return type for findBy methods
Solution
Step 1: Identify the correct property names
The properties are nested: customer.city and orderTotal. The method name must reflect this exactly.Step 2: Use correct keywords for comparison
For 'greater than', the keyword is 'GreaterThan' in Spring Data JPA method names.Step 3: Combine conditions with 'And'
The method name should combine both conditions with 'And' and use full property paths.Final Answer:
findByCustomerCityAndOrderTotalGreaterThan(String city, double total); -> Option AQuick Check:
Use full property names + GreaterThan + And [OK]
- Using incomplete property names
- Using 'Greater' instead of 'GreaterThan'
- Omitting 'By' after 'find'
