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...,因此导致 电子邮件的问题 字段在实际调用之前自动更新。 下面是日志文件

enter image description here

如何只更新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;
}