如何解决CrudRepository 自动更新自定义更新中的其他字段
我在 Spring 启动应用程序中使用 CrudRepository、MysqL。我有如下类帐户
@Entity
@Table(name="account")
public class Account {
@Column(name="account_id")
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long accountId;
@Column(name="username",unique = true)
private String username;
@Column(name="email",unique = true)
private String email;
@Column(name="password")
private String password;
@Column(name="fname")
private String fname;
@Column(name="lname")
private String lname;
@Column(name="birth_date")
@Temporal(TemporalType.DATE)
private Date birth_date;
我的 AccountController 接受一些参数并更新类 Account 的实例
@PutMapping("/update/{username}")
@ResponseBody
public Boolean updateAccount(@PathVariable String username,@RequestParam(name="email",required = false) String email,@RequestParam(name="lname",required = false) String lname,@RequestParam(name="fname",required = false) String fname,@RequestParam(name="birth_date",required = false) String birth_date,@RequestParam(name="password",required = false) String password){
Account account = accountService.getAccountByUsername(username);
if (email!= null) account.setEmail(email);
if (lname!= null) account.setLname(lname);
if (fname!= null) account.setFname(fname);
if (password!= null) account.setPassword(password);
if (birth_date!= null) {
// DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
System.out.println("Account Controller");
accountService.updateBirthDate(account,birth_date);
}
if (!accountService.checkAccountExistByEmail(email)) {
accountService.updateAccount(account);
return true;
}else return false;
}
我更新birth_date的AccountService方法是这样的
public void updateBirthDate(Account account,String date){
System.out.println("Account Service");
accountRepository.updateBD(date,account.getId());
}
还有我的自定义存储库
@Modifying
@Transactional
@Query(value = "UPDATE account SET birth_date =:date WHERE account_id =:id",nativeQuery = true)
void updateBD(@Param("date") String date,@Param("id") Long id);
似乎一切正常。您可以看到我的 AccountController,我在 checkExistemail 之前调用了方法 updateBirthday。但是当我第一次使用完整参数更新帐户时,Repository 中的方法 updateBD 会自动保存更新后的帐户以及其他字段,例如电子邮件、lname、fname...,因此导致 电子邮件的问题 字段在实际调用之前自动更新。 下面是日志文件
如何只更新birth_date而不更新其他字段。
解决方法
虽然我在代码中看到了一些非常规的东西,但是,您的问题的直接解决方案是:
@PutMapping("/update/{username}")
@ResponseBody
public Boolean updateAccount(@PathVariable String username,@RequestParam(name="email",required = false) String email,@RequestParam(name="lname",required = false) String lname,@RequestParam(name="fname",required = false) String fname,@RequestParam(name="birth_date",required = false) String birth_date,@RequestParam(name="password",required = false) String password){
Account account = accountService.getAccountByUsername(username);
if (birth_date!= null) {
// DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
System.out.println("Account Controller");
accountService.updateBirthDate(account,birth_date);
}
if (email!= null) account.setEmail(email);
if (lname!= null) account.setLname(lname);
if (fname!= null) account.setFname(fname);
if (password!= null) account.setPassword(password);
if (!accountService.checkAccountExistByEmail(email)) {
accountService.updateAccount(account);
return true;
}else return false;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。