Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a custom query method by naming convention in Spring Boot?
It is a method in a Spring Data repository interface whose name follows a pattern that Spring Data understands to create a database query automatically without writing SQL.
Click to reveal answer
intermediate
How does Spring Boot know what query to run from a method name?
Spring Data parses the method name to identify keywords like findBy, countBy, and conditions like And, Or, GreaterThan, then builds the query based on the entity's fields.
Click to reveal answer
beginner
Example: What does the method name findByLastNameAndAgeGreaterThan mean?
It means find all records where the lastName field matches the given value AND the age field is greater than the given number.
Click to reveal answer
intermediate
Can you use custom query methods for sorting and limiting results?
Yes, by adding keywords like OrderBy and using Pageable parameters, you can sort and limit results using naming conventions.
Click to reveal answer
beginner
What happens if a method name does not follow Spring Boot's naming conventions?
Spring Data will throw an error or fail to create the query because it cannot understand how to translate the method name into a database query.
Click to reveal answer
Which prefix is used to create a method that returns a list of entities matching a condition?
AupdateBy
BdeleteBy
CinsertBy
DfindBy
✗ Incorrect
The prefix 'findBy' tells Spring Data to find and return entities matching the condition.
What does the method name countByStatus do?
ADeletes entities with a given status
BCounts entities with a given status
CFinds entities with a given status
DUpdates entities with a given status
✗ Incorrect
'countBy' counts how many entities match the condition.
Which keyword would you use to combine two conditions in a method name?
AAnd
BOr
CNot
DBetween
✗ Incorrect
'And' combines two conditions that both must be true.
How do you specify sorting in a custom query method name?
AAdd 'GroupBy' followed by the field name
BAdd 'SortBy' followed by the field name
CAdd 'OrderBy' followed by the field name
DAdd 'FilterBy' followed by the field name
✗ Incorrect
'OrderBy' is used to specify sorting in the method name.
What will happen if you name a method findAllUsers in a Spring Data repository?
ASpring Data will not recognize it as a query method
BIt will find all users
CIt will delete all users
DIt will update all users
✗ Incorrect
The method name does not follow the naming convention for queries, so Spring Data cannot create a query.
Explain how Spring Boot creates database queries from custom method names in repositories.
Think about how the method name tells Spring Boot what to look for in the database.
You got /5 concepts.
Describe how you would write a method name to find all records where age is greater than 30 and status is 'active'.
Combine conditions with 'And' and use comparison keywords.
You got /5 concepts.
Practice
(1/5)
1. What does a Spring Data JPA method named findByLastName do?
easy
A. Counts records where the lastName matches the given value
B. Deletes records where the lastName matches the given value
C. Checks if any record exists with the given lastName
D. Finds all records where the lastName matches the given value
Solution
Step 1: Understand the method prefix
The prefix findBy in Spring Data JPA means it will search and return matching records.
Step 2: Analyze the property name
The method uses LastName as 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 D
Quick Check:
findBy + property = find matching records [OK]
Hint: findBy means search and return matching records [OK]
Common Mistakes:
Confusing findBy with deleteBy or countBy
Thinking it returns a boolean instead of records
Ignoring the property name after findBy
2. Which of the following is the correct syntax for a method that counts users by their age in Spring Data JPA?
easy
A. findCountByAge(int age);
B. countUsersByAge(int age);
C. countByAge(int age);
D. countAgeBy(int age);
Solution
Step 1: Identify the correct prefix for counting
The correct prefix to count records is countBy.
Step 2: Check method naming pattern
The method should be countByAge to count users filtered by age.
Final Answer:
countByAge(int age); -> Option C
Quick Check:
countBy + property = count matching records [OK]
Hint: Use countBy + property to count records [OK]
Common Mistakes:
Adding extra words like Users in method name
Using findCountBy which is invalid
Placing property name after count instead of after By
3. Given the method existsByEmailAndStatus(String email, String status), what will it return if a user with email "test@example.com" and status "active" exists?
medium
A. A list of users matching the email and status
B. true
C. false
D. The count of users matching the email and status
Solution
Step 1: Understand the method prefix
The prefix existsBy returns 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 with And.
Final Answer:
true -> Option B
Quick Check:
existsBy + conditions returns boolean [OK]
Hint: existsBy returns true if matching record exists [OK]
Common Mistakes:
Expecting a list instead of boolean
Confusing existsBy with findBy
Ignoring the combined conditions with And
4. Identify the error in this Spring Data JPA method declaration: List<User> findByNameOr(int age, String name);
medium
A. The method name is missing the property after 'Or'
B. The method should use 'And' instead of 'Or' for combining conditions
C. The order of parameters does not match the method name conditions
D. The return type should be boolean for 'findBy' methods
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 A
Quick Check:
Method names must specify property after 'Or' [OK]
Hint: After 'Or' or 'And', always specify property name [OK]
Common Mistakes:
Leaving out property name after 'Or' or 'And'
Mixing parameter order with method name order
Using wrong return type for findBy methods
5. You want to create a method that finds all orders where the customer's city is "New York" and the order total is greater than 100. Which method name correctly follows Spring Data JPA naming conventions?
hard
A. findByCustomerCityAndOrderTotalGreaterThan(String city, double total);
B. findOrdersByCityAndTotalGreater(String city, double total);
C. findByCityAndOrderTotalGreaterThan(String city, double total);
D. findByCustomerCityAndOrderTotalGreater(String city, double total);
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 A
Quick Check:
Use full property names + GreaterThan + And [OK]
Hint: Use full property names and 'GreaterThan' for > comparisons [OK]