Complete the code to define a native SQL query in a Spring Data JPA repository.
@Query(value = "SELECT * FROM users WHERE id = [1]", nativeQuery = true) User findUserById(Long id);
The placeholder ?1 refers to the first method parameter in native SQL queries.
Complete the code to execute a native SQL update query in a Spring Data JPA repository.
@Modifying @Query(value = "UPDATE products SET price = [1] WHERE id = ?1", nativeQuery = true) int updatePriceById(Long id, Double price);
The placeholder ?2 refers to the second method parameter, which is price here.
Fix the error in the native query to select users with a specific role.
@Query(value = "SELECT * FROM users WHERE role = [1]", nativeQuery = true) List<User> findUsersByRole(String role);
In native queries, positional parameter ?1 correctly refers to the first method argument.
Fill both blanks to create a native query that deletes products by category and returns affected rows.
@Modifying @Query(value = "DELETE FROM products WHERE category = [1] AND available = [2]", nativeQuery = true) int deleteByCategoryAndAvailability(String category, boolean available);
Use positional parameters ?1 and ?2 for the first and second method arguments respectively in native queries.
Fill all three blanks to write a native query that selects users by status and orders by creation date.
@Query(value = "SELECT * FROM users WHERE status = [1] ORDER BY [2] [3]", nativeQuery = true) List<User> findUsersByStatusOrdered(String status, String orderBy, String direction);
The first blank uses positional parameter ?1 for status. The second and third blanks are column name and order direction respectively.