在ObjectId字段中存储自动增量值时出现的问题

时间:2018-10-10 22:32:38

标签: mongodb spring-boot

当我尝试实现代码以自动增加字段值时遇到问题。请帮助。

这是代码段

public ModelName create(@ModelAttribute ModelName modelName) {
    newEmployee.setEmployeeId(this.getNextSequence("counter"));
}
    public long getNextSequence(String seqName) {
        ModelName employeeIdCounter = mongoOp.findAndModify(query(where("_id").is(seqName)),
                new Update().inc("employeeId", 1), options().returnNew(true).upsert(true), ModelName .class);
        return employeeIdCounter.getEmployeeId();
    }

错误堆栈:

2018-10-10 15:10:37.229 ERROR 9936 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [org.bson.types.ObjectId] for value 'counter'; nested exception is java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [counter]] with root cause

java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [counter]
    at org.bson.types.ObjectId.parseHexString(ObjectId.java:549) ~[bson-3.6.4.jar:na]
    at org.bson.types.ObjectId.<init>(ObjectId.java:239) ~[bson-3.6.4.jar:na]
    at org.springframework.data.mongodb.core.convert.MongoConverters$StringToObjectIdConverter.convert(MongoConverters.java:115) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.mongodb.core.convert.MongoConverters$StringToObjectIdConverter.convert(MongoConverters.java:111) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]

0 个答案:

没有答案