0
0
Spring Bootframework~20 mins

@Query for custom JPQL in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JPQL Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
}
AA list containing 2 User objects with active = false
BA list containing all 5 User objects regardless of active status
CAn empty list
DA list containing 3 User objects with active = true
Attempts:
2 left
💡 Hint
Look at the WHERE clause in the JPQL query.
📝 Syntax
intermediate
2: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?
A@Query("SELECT u FROM User u WHERE u.email == :email")
B@Query("SELECT * FROM User WHERE email = :email")
C@Query("SELECT u FROM User u WHERE u.email = :email")
D@Query("SELECT u FROM User u WHERE u.email = ?1")
Attempts:
2 left
💡 Hint
JPQL uses entity names and fields, not table names or SQL syntax.
🔧 Debug
advanced
2: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?
Aorg.springframework.dao.InvalidDataAccessApiUsageException due to parameter index out of bounds
BNo error, method works correctly
CSyntax error in JPQL query
DNullPointerException at runtime
Attempts:
2 left
💡 Hint
Check the parameter index used in the query and method signature.
🧠 Conceptual
advanced
2:00remaining
Which statement about @Query and JPQL is true?
Select the correct statement about using @Query with JPQL in Spring Data repositories.
A@Query cannot use parameters and must hardcode values.
B@Query allows defining JPQL queries that use entity names and fields, not table names.
C@Query queries must always use native SQL syntax.
D@Query queries are automatically converted to REST endpoints.
Attempts:
2 left
💡 Hint
Think about what JPQL queries look like compared to SQL.
state_output
expert
3: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?
AA list of 2 User objects who have at least one order with status 'SHIPPED'
BA list of all users regardless of order status
CA list of 1 User object with orders status 'PENDING'
DAn empty list
Attempts:
2 left
💡 Hint
The query joins users with their orders and filters by order status.