Home > other >  Java - Spring mongorepository save repeat key exception thrown
Java - Spring mongorepository save repeat key exception thrown

Time:11-18

I'm using Java and Spring. As a test, I by id query object, then try to save the same object and not update anything. When I do this, I get a duplicate key anomaly. According to the content, I read if _id is null, the MongoRepository. The save () should be an insert, otherwise should perform an update. Obviously, I should be updated.
A bit of code:

//Succeeds
Datatype sut=mongoRepository. FindOne (" 569 eac0dd4c623dc65508679 ");

//Fails with the duplicate key.
MongoRepository. Save (sut);

Why is that? Repeat the other classes of objects, their work. How can I shoot this trouble? I don't know how to break down and solve problems.
Thank you
Error:

HTTP - bio - 8080-27906 [exec - 3] the 13:00:26 2016-05-02, 304 the DEBUG org. Springframework. Web. Servlet. MVC) method. The annotation. ExceptionHandlerExceptionResolver -

Resolving exception from handler

Public gov. Nist. Healthcare. Tools.. Hl7 v2. Igamt. Lite. Web. DatatypeSaveResponse
Gov. Nist. Healthcare. Tools.. Hl7 v2. Igamt. Lite. Web. Controller. DatatypeController. Save (
Gov. Nist. Healthcare. Tools. Hl7. V2. Igamt. Lite. Domain. The Datatype)
Throws gov. Nist. Healthcare. Tools.. Hl7 v2. Igamt. Lite. Web. Exception. DatatypeSaveException
] :
Org. Springframework. Dao. DuplicateKeyException: {
"ServerUsed" : "127.0.0.1:27017,"
"Ok" : 1,
"N" : 0,
"Err" : "E11000 duplicate key error index: igl. Datatype. $_id_ dup key: {: ObjectId (' 569 eac0dd4c623dc65508679 ')}",
"Code" : 11000};
Nested exception is com. Mongo. MongoException $DuplicateKey: {
"ServerUsed" : "127.0.0.1:27017,"
"Ok" : 1,
"N" : 0,
"Err" : "E11000 duplicate key error index: igl. Datatype. $_id_ dup key: {: ObjectId (' 569 eac0dd4c623dc65508679 ')}",
"Code" : 11000}

. Repeat
I have just found a. As shown above, when saving spring will attempt to insert, even fill the _id.
Save the other objects (not shown), spring, update, and once again fill is _id.
Why is different? Document said spring should be updated when filling _id, and without filling insertion.
What can cause this? What is my object. Maybe I read converter?
Update: I've just seen the team. After careful review, we make sure we don't need to read converter. Through another way to solve the problem.

CodePudding user response:

On the database side, you may have to create a unique index. Please see "https://docs.mongodb.com/manual/core/index-unique/" for more information.

CodePudding user response:

The original post http://www.voidcn.com/article/p-nyuqcjru-byq.html
  • Related