在编辑表单时,用户有时可能不更改表单而仍然单击提交按钮。在下面的控制器方法之一中,即使用户没有更改任何内容,save() 方法也会对数据库执行查询并更新字段吗?
PostMapping("/edit_entry/{entryId}")
public String update_entry(
@PathVariable("entryId") Long entryId,
@RequestParam String title,
@RequestParam String text
) {
Entry entry = this.entryRepo.findById(entryId).get();
if (!entry.getTitle().equals(title))
entry.setTitle(title);
if (!entry.getText().equals(text))
entry.setText(text);
this.entryRepo.save(entry);
return "redirect:/entries";
}
Run Code Online (Sandbox Code Playgroud)
另外,在这种情况下“if”语句是否必要?
spring hibernate hibernate-mapping spring-data spring-data-jpa