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 the purpose of the @Query annotation in Spring Data JPA?
The @Query annotation lets you write custom JPQL or SQL queries directly in your repository methods to fetch data in ways not covered by method names.
Click to reveal answer
beginner
How do you write a simple JPQL query using @Query to find users by their email?
You write @Query("SELECT u FROM User u WHERE u.email = :email") above the repository method, and use @Param("email") to bind the method parameter.
Click to reveal answer
intermediate
Can @Query use native SQL queries instead of JPQL? How?
Yes, by adding nativeQuery = true in the @Query annotation, you can write native SQL queries instead of JPQL.
Click to reveal answer
intermediate
What happens if you omit the @Param annotation in a @Query method with named parameters?
Spring Data JPA may not bind the method parameters correctly to the query parameters, causing errors or unexpected results.
Click to reveal answer
intermediate
How does @Query improve flexibility compared to derived query methods in Spring Data JPA?
It allows writing complex queries with joins, aggregations, or conditions that are hard or impossible to express with method names alone.
Click to reveal answer
What does the @Query annotation in Spring Data JPA allow you to do?
AWrite custom JPQL or SQL queries
BAutomatically generate database tables
CConfigure database connections
DCreate REST endpoints
✗ Incorrect
The @Query annotation lets you write custom JPQL or SQL queries directly in repository methods.
How do you specify a native SQL query in @Query?
AUse @NativeQuery annotation
BAdd nativeQuery = true in the annotation
CWrite SQL without any extra flags
DSet native = true in application.properties
✗ Incorrect
You add nativeQuery = true in the @Query annotation to use native SQL.
Which keyword is used in JPQL to refer to an entity?
ADATABASE
BTABLE
CFROM
DCOLLECTION
✗ Incorrect
JPQL uses FROM to specify the entity to query.
What annotation should you use to bind method parameters to named parameters in @Query?
A@QueryParam
B@Bind
C@Named
D@Param
✗ Incorrect
Use @Param to bind method parameters to named query parameters.
If you want to write a query with a join in Spring Data JPA, which is the best approach?
AUse @Query with JPQL
BUse method name conventions only
CWrite SQL in application.properties
DUse @JoinColumn annotation
✗ Incorrect
Using @Query with JPQL allows writing joins easily.
Explain how to use the @Query annotation to write a custom JPQL query with parameters in Spring Data JPA.
Think about how you write the JPQL and connect it to method inputs.
You got /4 concepts.
Describe the difference between JPQL and native SQL queries when using @Query in Spring Data JPA.
Consider how the query language relates to the database and entities.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @Query annotation in Spring Data JPA?
easy
A. To write custom JPQL or SQL queries when default methods are insufficient
B. To automatically generate database tables
C. To configure database connection properties
D. To define entity relationships
Solution
Step 1: Understand default query methods
Spring Data JPA provides default query methods like findById, but they are limited.
Step 2: Role of @Query
@Query allows writing custom JPQL or SQL queries to handle complex or specific data retrieval needs.
Final Answer:
To write custom JPQL or SQL queries when default methods are insufficient -> Option A
Quick Check:
@Query purpose = custom queries [OK]
Hint: Remember @Query is for custom queries beyond defaults [OK]
Common Mistakes:
Thinking @Query creates tables
Confusing @Query with database config
Assuming @Query defines entity relations
2. Which of the following is the correct syntax to define a custom JPQL query using @Query in a Spring Data JPA repository interface?
easy
A. @Query("SELECT u FROM User u WHERE u.name = :name") List<User> findByName(@Param("name") String name);
B. @Query(SELECT * FROM User WHERE name = :name) List<User> findByName(String name);
C. @Query("SELECT * FROM User WHERE name = ?1") List<User> findByName(String name);
D. @Query("FROM User WHERE name = ?") List<User> findByName(@Param("name") String name);
Solution
Step 1: Check JPQL syntax
JPQL uses entity names and fields, not table names or * syntax.
Step 2: Parameter binding
Named parameters use :paramName and must be linked with @Param("paramName") in method.
Final Answer:
@Query("SELECT u FROM User u WHERE u.name = :name") List<User> findByName(@Param("name") String name); -> Option A
Quick Check:
JPQL + named param + @Param = correct syntax [OK]
Hint: Use entity names and :param with @Param for correct JPQL [OK]
Common Mistakes:
Using SQL syntax (*) instead of JPQL
Missing @Param annotation for named parameters
Using positional parameters incorrectly
3. Given the repository method:
@Query("SELECT u FROM User u WHERE u.age > :minAge")
List<User> findUsersOlderThan(@Param("minAge") int minAge);
What will be the result of calling findUsersOlderThan(30)?
medium
A. A list of User entities with age less than 30
B. A list of User entities with age equal to 30
C. A list of User entities with age greater than 30
D. A runtime error due to missing parameter
Solution
Step 1: Analyze the JPQL query
The query selects users where age is greater than the parameter minAge.
Step 2: Understand method call
Calling findUsersOlderThan(30) sets minAge to 30, so users older than 30 are returned.
Final Answer:
A list of User entities with age greater than 30 -> Option C
Quick Check:
minAge=30, query > minAge = users older than 30 [OK]
Hint: Check parameter value and query condition carefully [OK]
Common Mistakes:
Confusing > with >= or =
Assuming parameter is ignored
Expecting users younger than 30
4. Identify the error in the following repository method:
@Query("SELECT u FROM User u WHERE u.email = :email")
List<User> findByEmail(String email);
medium
A. JPQL query uses wrong entity name
B. Missing @Param annotation for the email parameter
C. Return type should be User, not List<User>
D. Query should use native SQL syntax
Solution
Step 1: Check parameter binding
The query uses a named parameter :email, so the method parameter must have @Param("email") annotation.
Step 2: Validate other parts
Entity name User is correct, return type List<User> is valid, and JPQL syntax is correct.
Final Answer:
Missing @Param annotation for the email parameter -> Option B
Quick Check:
Named param requires @Param annotation [OK]
Hint: Always add @Param for named parameters in @Query [OK]
Common Mistakes:
Omitting @Param causes runtime errors
Confusing JPQL with native SQL
Assuming return type must be single entity
5. You want to write a custom JPQL query using @Query to find all users whose name contains a given substring (case insensitive). Which of the following method definitions correctly achieves this?
hard
A. @Query("SELECT u FROM User u WHERE u.name LIKE :namePart") List<User> findByNameLike(@Param("namePart") String namePart);
B. @Query("SELECT u FROM User u WHERE u.name LIKE '%:namePart%'") List<User> findByNameContains(@Param("namePart") String namePart);
C. @Query("SELECT u FROM User u WHERE u.name = :namePart") List<User> findByNameExact(@Param("namePart") String namePart);
D. @Query("SELECT u FROM User u WHERE LOWER(u.name) LIKE LOWER(CONCAT('%', :namePart, '%'))") List<User> findByNameContainsIgnoreCase(@Param("namePart") String namePart);
Solution
Step 1: Understand case insensitive search
Use LOWER() on both field and parameter to ignore case.
Step 2: Use LIKE with wildcards
Concatenate '%' before and after parameter to find substring matches.
Step 3: Check parameter binding
Named parameter :namePart is linked with @Param("namePart") correctly.
Final Answer:
@Query("SELECT u FROM User u WHERE LOWER(u.name) LIKE LOWER(CONCAT('%', :namePart, '%'))") List<User> findByNameContainsIgnoreCase(@Param("namePart") String namePart); -> Option D