I am trying to update a field on my Object and then trying to save it. The code is like this in the controller that will be called.
ApplicationUser user = applicationUserRepository.findByVerificationCode(verificationCode);
if(user != null) {
user.setVerified(true);//trying to change a value in a field
applicationUserRepository.save(user);
return new ResponseEntity<>(user,new HttpHeaders(),HttpStatus.OK);
}
When I try to execute this code, I get this error
E11000 duplicate key error index: myapp.applicationUser.$id dup key: { : 0 };
I am defining Id explicitly in the ApplicationUser class. My ApplicationUser class is like this
public class ApplicationUser {
@Id
private long id;
private String username;
private String password;
private String name;
private String email;
private String verificationCode;
private boolean verified=false;
private List<Company> boughtCompanies;
public long getId() {
return id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getVerificationCode() {
return verificationCode;
}
public void setVerificationCode(String verificationCode) {
this.verificationCode = verificationCode;
}
public List<Company> getBoughtCompanies() {
return boughtCompanies;
}
public void setBoughtCompanies(List<Company> boughtCompanies) {
this.boughtCompanies = boughtCompanies;
}
public boolean isVerified() {
return verified;
}
public void setVerified(boolean verified) {
this.verified = verified;
}
}
What am I doing wrong here or how should I procced? Thanks.