-1

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.

4 Answers 4

0

you try to insert an existant user with the same id function insert i think you should change it with funcion of update

Sign up to request clarification or add additional context in comments.

2 Comments

I was using save but I was still getting the same error.
id of user must be generated
0

You didnt use the setter of your ID so you need to put it as auto increment

public class ApplicationUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    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;
    }       
}

Comments

0

Edit: Set the @Id field as String. It is not good to have long values as ID in mongo. Also as M. Wajdi said you need to add the setter to the id field.


I see that you are performing an update of a document so you should use applicationUserRepository.save() instead of applicationUserRepository.insert(). (Actually, I always use save).

Explanation:

Insert always try to introduce a new object in the database. The first time you create the ApplicationUser, insert creates the object in the DB and assign it an ID. But if you read it, update it and try to insert it again, Mongo will understand that you are actually trying to introduce a new object with the same ID (instead of performing an update in that object). That's why you get that exception, duplicated key.

3 Comments

Sorry, I was using save from the beginning and still, I was getting the same error. I tried with insert but same problem. I have also updated the question
Could you attach the code of applicationUserRepository? Also, you can try to change the @Id field from long to String. The IDs generated by the client are normally UUIDs in String format.
I changed Id from Long to String and it worked. Thanks a lot.
0

While choosing long (or any other primitive) as mongo @Id, then you have to cater the id generation, else everytime it will give long default value i.e. 0

For autoincrement sequencing refer, already answered here Auto increment sequence in mongodb using java.

Else make @Id of type String, mongo autogenerates Default Id of hexadeximal type, for more info refer https://docs.mongodb.com/manual/reference/method/ObjectId/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.