'Unable to query from database using JPA in custom repository

I am getting this error whenI lauch

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in xxxxxxx.Repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract xxxxx.Model.User xxxxx.Repository.UserRepository.findByEmail(java.lang.String)! Reason: Validation failed for query for method public abstract xxxx.Model.User xxxxx.Repository.UserRepository.findByEmail(java.lang.String)!; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract xxxx.Model.User xxxxx.Repository.UserRepository.findByEmail(java.lang.String)

This is m repository:

public interface UserRepository extends JpaRepository<User, Long> {

     @Query("SELECT u FROM User u WHERE u.email = ?1")
     public User findByMail(String mail);
}

Whe is rong here?



Solution 1:[1]

You can try this one:

public interface UserRepository extends JpaRepository<User, Long> {
     public User findByMail(@Param("mail") String mail);
}

Solution 2:[2]

try this

 @Query("SELECT u FROM User u WHERE u.email =:mail",nativeQuery=true)
 public User findByMail(String mail);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mrt
Solution 2 vikas