Complete the code to define a custom JPQL query to find users by their email.
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email = [1]")
User findByEmail(String email);
}The correct syntax for named parameters in JPQL is :parameterName. Here, :email matches the method parameter email.
Complete the code to write a JPQL query that selects users with age greater than a given value.
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.age [1] :age")
List<User> findUsersOlderThan(int age);
}The query should select users whose age is greater than the given parameter, so the operator is >.
Fix the error in the JPQL query to correctly select users by their last name ignoring case.
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE LOWER(u.lastName) = [1]")
List<User> findByLastNameIgnoreCase(String lastName);
}JPQL supports the LOWER() function on parameters as well. To compare ignoring case, apply LOWER() to the parameter :lastName.
Fill both blanks to write a JPQL query that selects users whose first name starts with a given prefix.
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.firstName [1] :prefix")
List<User> findByFirstNameStartingWith([2] prefix);
}The JPQL operator for pattern matching is LIKE. The method parameter type for a prefix string is String.
Fill all three blanks to write a JPQL query that selects users with age between two values.
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.age BETWEEN [1] AND [2]")
List<User> findUsersBetweenAges([3] minAge, [3] maxAge);
}The JPQL BETWEEN clause uses named parameters :minAge and :maxAge. The method parameters for ages should be of type int.