Java – spring mongorepository save throws duplicate key exception

I'm using Java and spring As a test, I query the object by ID and try to save the same object without updating anything When I do this, I get a duplicate key exception According to what I've read, if_ If Id is null, mongorepository Save() should perform an insert, otherwise it should perform an update Obviously, I should get an update

One point code:

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

// Fails with duplicate key.
mongoRepository.save(sut);

Why? Repeat the objects of other classes above, and they work How can I bother to shoot this? I don't know how to decompose and solve the problem

thank you

Error:

27906 [http-bio-8080-exec-3] 2016-05-02 13:00:26,304 DEBUG org.springframework.web.servlet.mvc.method.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.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('569eac0dd4c623dc65508679') }","code" : 11000};
nested exception is com.mongodb.MongoException$DuplicateKey: {
  "serverUsed" : "127.0.0.1:27017","code" : 11000}

... repeat

I just found one When you save as shown above, spring will try to insert, even if it is filled_ So is ID

When you save other objects (not shown but similar), the spring performs, updates, and fills again_ id.

Why is it different? The document says the spring should be filled_ ID is updated and inserted when not populated

What else can cause this? What's in my object? Maybe my read converter?

Update: I just met this team After careful review, we determined that we no longer need to read the converter The problem is solved in another way

Solution

On the database side, you may have created a unique index Please check“ https://docs.mongodb.com/manual/core/index-unique/ ”Learn more

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>