使用 @Query 在 Spring 中更新表格

huangapple go评论69阅读模式
英文:

Use @Query to update table in Spring

问题

我正尝试在Spring中使用自定义查询来更新表,但是我遇到了404 NOT FOUND错误。

这是我的repository查询:

@Modifying
@Query("update AssignedFault af set af.userAssigned= :email where af.id= :id")
void allocateFault(@Param("email") String email, @Param("id") Long id);

这是我的controller方法:

@PutMapping("/assigned_faults/assign/{email}/{id}")
void assignFault(@PathVariable String email, @PathVariable Long id){
    assignedFaultRepository.allocateFault(email, id);
}
英文:

I am trying to update a table using a custom Query in Spring but I get a 404 NOT FOUND error

Here is my repository query

@Modifying
@Query("update AssignedFault af set af.userAssigned= :email where af.id= :id")
void allocateFault(@Param("email") String email, @Param("id") Long id);

Here is my controller method

@PutMapping("/assigned_faults/assign/{email}/{id}")
void assignFault(@PathVariable String email, @PathVariable Long id){
assignedFaultRepository.allocateFault(email, id);
}

答案1

得分: 0

除了修复方法代码如下所示...

  @PutMapping("/assigned_faults/assign")
  void assignFault(@RequestParam String email, @RequestParam Long id){
  assignedFaultRepository.allocateFault(email, id);  
}

...我还在我的存储库接口中添加了 @Transactional 注解。

英文:

On top of fixing the method code like this...

  @PutMapping("/assigned_faults/assign")
  void assignFault(@RequestParam String email, @RequestParam Long id){
  assignedFaultRepository.allocateFault(email, id);  
}

... I also added the @Transactional annotation to my repository interface

huangapple
  • 本文由 发表于 2020年9月22日 18:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64007633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定