Vannian
Vannian

Reputation: 1530

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?

Upvotes: 0

Views: 125

Answers (2)

vikas
vikas

Reputation: 68

try this

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

Upvotes: 0

Murat
Murat

Reputation: 440

You can try this one:

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

Upvotes: 1

Related Questions