在Spring Boot Thymeleaf中更新密码

时间:2018-10-20 12:00:26

标签: spring-boot nativequery

我想通过Thymeleaf更新密码。 到目前为止,一切正常,除了调用更新方法后,我会收到一条错误消息。

这是我的代码:

@PostMapping
public String updateOldPassword(@Valid @ModelAttribute("password") PasswordChange userAndpassword,
            BindingResult result, ModelMap model) {

    if (result.hasErrors()) {
        return "error";
    }

    String username = userAndpassword.getUsername();
    String password = userAndpassword.getPassword();

    String newPassword = passwordEncoder.encode(password);

    passwordRepo.updatePassword(username, newPassword);
    return "redirect:/";
}

public interface updateOldPasswordRepository extends JpaRepository<Benutzer, Long>{
    @Modifying
    @Query(value = "UPDATE benutzer SET passwort = ? WHERE benutzername = ?", nativeQuery = true)
    void updatePassword(String benutzername, String passwort);
}

当它尝试更新密码时,出现以下错误消息:

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query] with root cause
javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1496)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:256)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)

也许有人可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

我发现了问题。 我不得不使用

@Transactional

也是。

这是完整的代码:

public interface updateOldPasswordRepository extends JpaRepository<Benutzer, Long>{
    @Modifying
    @Query(value = "UPDATE benutzer SET passwort = :passwort WHERE benutzername = :benutzername", nativeQuery = true)
    @Transactional
    void updatePassword(@Param("benutzername") String benutzername, @Param("passwort") String passwort);
}