Challenge - 5 Problems
JPQL Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this JPQL query method?
Given the following Spring Data repository method using @Query, what will be the result when calling
findActiveUsers() if the database has 3 users with active = true and 2 users with active = false?Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.active = true")
List<User> findActiveUsers();
}Attempts:
2 left
💡 Hint
Look at the WHERE clause in the JPQL query.
✗ Incorrect
The query selects users where the 'active' field is true, so only users with active = true are returned.
📝 Syntax
intermediate2:00remaining
Which @Query syntax is correct for selecting users by email?
You want to write a JPQL query to find a user by their email address. Which of the following @Query annotations is syntactically correct?
Attempts:
2 left
💡 Hint
JPQL uses entity names and fields, not table names or SQL syntax.
✗ Incorrect
JPQL queries use entity names and fields, and parameters are referenced with a colon and name. '==' is invalid in JPQL, and '*' is not used.
🔧 Debug
advanced2:00remaining
What error does this @Query cause?
Consider this repository method:
@Query("SELECT u FROM User u WHERE u.name = ?2")
User findByName(String name);
What error will occur when this method is called?Attempts:
2 left
💡 Hint
Check the parameter index used in the query and method signature.
✗ Incorrect
The query uses parameter index 2, but the method has only one parameter, so Spring throws an exception about invalid parameter index.
🧠 Conceptual
advanced2:00remaining
Which statement about @Query and JPQL is true?
Select the correct statement about using @Query with JPQL in Spring Data repositories.
Attempts:
2 left
💡 Hint
Think about what JPQL queries look like compared to SQL.
✗ Incorrect
JPQL queries use entity names and their fields, not database table names. @Query supports JPQL and native queries if specified.
❓ state_output
expert3:00remaining
What is the result of this @Query with JOIN and parameter?
Given these entities: User and Order, where User has a collection of Orders. Consider this repository method:
@Query("SELECT u FROM User u JOIN u.orders o WHERE o.status = :status")
List findUsersByOrderStatus(@Param("status") String status);
If the database has 2 users with orders of status 'SHIPPED' and 1 user with orders of status 'PENDING', what does calling findUsersByOrderStatus("SHIPPED") return?Attempts:
2 left
💡 Hint
The query joins users with their orders and filters by order status.
✗ Incorrect
The query returns users who have orders matching the given status. So users with 'SHIPPED' orders are returned.