Home > Enterprise >  Is there a JpaRepository method which can save a provided password where username condition?
Is there a JpaRepository method which can save a provided password where username condition?

Time:12-05

I am looking for a JpaRepository method to update a user provided password at the time of resetting it with a username condition . Thanks in advance

CodePudding user response:

Have a look at creating custom query methods with JPA as per documentation: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query.

For your use case, the following would allow you update the correct user's password:

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {

  @Query("update User u set u.password= ?1 where u.username = ?2")
  void updatePassword(String username, String password);

}

CodePudding user response:

In Spring Data JPA, whenever you execute an update, you need to use the @Modifying along with the @Query. Failing to use the @Modifying will result in InvalidDataAccessApiUsage exception.

Find the code below

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {

  @Modifying
  @Query("update User u set u.password= :password where u.username = :username")
  void updatePassword(@Param("username") String username, @Param("password") String password);

}
  • Related