我正在使用 java 和 Spring。作为测试,我通过 id 查询一个对象,然后尝试保存同一个对象而不更新任何内容。当我这样做时,我得到一个重复的键异常。根据我读过的内容,如果 _id 为空,则 MongoRepository.save() 应该执行插入操作,否则执行更新操作。显然,我应该得到更新。
一点代码:
// Succeeds
Datatype sut = mongoRepository.findOne("569eac0dd4c623dc65508679");
// Fails with duplicate key.
mongoRepository.save(sut);
Run Code Online (Sandbox Code Playgroud)
为什么?对其他类的对象重复上述操作,它们就可以工作了。我怎样才能解决这个问题?我不知道如何分解它并隔离问题。
谢谢
错误:
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 …
Run Code Online (Sandbox Code Playgroud)