Issue
I have a problem with Spring repository.
I'm defining a query method and it's not available in repository instance.
Do I have to implement this method queries or add any dependencies?
UserRepository interface:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findUserByEmailAndPassword(String email, String password);
}
method where I want to execute this query method:
@PostMapping("/login")
public String loginUser(@ModelAttribute("user") User user, BindingResult bindingResult, Model model, HttpSession session){
if(bindingResult.hasErrors())
return "/login";
User loggedUser = userService.getUserRepository().findUserByEmailAndPassword("email", "password");
}
And i get the message Cannot resolve method 'findUserByEmailAndPassword' in 'JpaRepository'
. I tried to add @Query
annotation with sql query parameter but it didn't change a thing
Solution
It's difficult to tell from your code, but based on the error message you're seeing I'm assuming that the return type of userService.getUserRepository()
is JpaRepository
, but findUserByEmailAndPassword
is declared in UserRepository
.
If that's the case, you will need to either cast the result of userService.getUserRepository()
or change the return type to UserRepository
.
Answered By - David DeMar
Answer Checked By - David Goodson (JavaFixing Volunteer)