Reputation: 29
I'm using CrudRepository, Mysql in my Spring boot application. I have class Account like below
@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;
My AccountController takes some parameters and updates instance of class 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;
}
my AccountService method to update birth_date is something like this
public void updateBirthDate(Account account, String date){
System.out.println("Account Service");
accountRepository.updateBD(date, account.getId());
}
And my custom repository
@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);
Everything seems work fine. You can see my AccountController, i called method updateBirthday before checkExistEmail. But when the first time i updated Account with full parameters, the method updateBD in Repository auto save updated account with other fields like email, lname, fname... so it leads to problem that email field auto update before actually called.
Below is logs file
How can i update only birth_date without update other fields.
Upvotes: 0
Views: 311
Reputation: 146
Although there are some unconventional things I see in the code, But, immediate solution to your problem would be:
@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;
}
Upvotes: 1