【发布时间】:2019-04-13 23:39:36
【问题描述】:
我刚开始一个新项目,想使用 Sprint Boot 2.1,一开始就遇到了问题。我想做的是使用 Spring Boot Mongo 来管理数据库。我想要一个带有@Version 注释的乐观锁。但是,我发现 @Version 似乎会影响 MongoRepository 中的 save() 行为,这意味着 dup key 错误。
以下是示例代码。
POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Person {
@Id public ObjectId id;
@CreatedDate public LocalDateTime createOn;
@LastModifiedDate public LocalDateTime modifiedOn;
@Version public long version;
private String name;
private String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public String toString() {
return String.format("Person [id=%s, name=%s, email=%s, createdOn=%s, modifiedOn=%s, version=%s]", id, name, email, createOn, modifiedOn, version);
}
}
MongoConfig
@Configuration
@EnableMongoRepositories("com.project.server.repo")
@EnableMongoAuditing
public class MongoConfig {
}
存储库
public interface PersonRepo extends MongoRepository<Person, ObjectId> {
Person save(Person person);
Person findByName(String name);
Person findByEmail(String email);
long count();
@Override
void delete(Person person);
}
如Official Doc 中所示,我在long 中有我的version 字段,但是在第二个save 发生了dup key 错误,这意味着它再次尝试insert,即使id 在物体。
我还尝试在version 字段中使用Long,它没有发生dup 键并按预期保存为更新,但createdOn 在第一个save 中变为null(这意味着insert)
控制器
Person joe = new Person("Joe", "aa@aa.aa");
System.out.println(joe.toString());
this.personRepo.save(joe);
Person who = this.personRepo.findByName("Joe");
System.out.println(who.toString());
who.setEmail("bb@bb.bb");
this.personRepo.save(who);
Person who1 = this.personRepo.findByName("Joe");
Person who2 = this.personRepo.findByEmail("bb@bb.bb");
System.out.println(who1.toString());
System.out.println(who2.toString());
使用 dup 键记录 (long version)
2018-11-11 02:09:31.435 INFO 4319 --- [on(6)-127.0.0.1] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:186}] to localhost:27017
Person [id=null, name=Joe, email=aa@aa.aa, createdOn=null, modifiedOn=null, version=0]
2018-11-11 02:09:37.254 DEBUG 4319 --- [nio-8080-exec-1] o.s.data.auditing.AuditingHandler : Touched Person [id=null, name=Joe, email=aa@aa.aa, createdOn=2018-11-11T02:09:37.252, modifiedOn=2018-11-11T02:09:37.252, version=0] - Last modification at 2018-11-11T02:09:37.252 by unknown
2018-11-11 02:09:37.259 DEBUG 4319 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [createOn, modifiedOn, version, name, email, _class] in collection: person
2018-11-11 02:09:37.297 DEBUG 4319 --- [nio-8080-exec-1] o.s.d.m.r.query.MongoQueryCreator : Created query Query: { "name" : "Joe" }, Fields: { }, Sort: { }
2018-11-11 02:09:37.304 DEBUG 4319 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : find using query: { "name" : "Joe" } fields: Document{{}} for class: class com.seedu.server.model.Person in collection: person
Person [id=5be71ee11ad34410df06852c, name=Joe, email=aa@aa.aa, createdOn=2018-11-11T02:09:37.252, modifiedOn=2018-11-11T02:09:37.252, version=0]
2018-11-11 02:09:37.323 DEBUG 4319 --- [nio-8080-exec-1] o.s.data.auditing.AuditingHandler : Touched Person [id=5be71ee11ad34410df06852c, name=Joe, email=bb@bb.bb, createdOn=2018-11-11T02:09:37.323, modifiedOn=2018-11-11T02:09:37.323, version=0] - Last modification at 2018-11-11T02:09:37.323 by unknown
2018-11-11 02:09:37.324 DEBUG 4319 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [_id, createOn, modifiedOn, version, name, email, _class] in collection: person
org.springframework.dao.DuplicateKeyException: E11000 duplicate key error collection: seedu.person index: _id_ dup key: { : ObjectId('5be71ee11ad34410df06852c') }; nested exception is com.mongodb.MongoWriteException: E11000 duplicate key error collection: seedu.person index: _id_ dup key: { : ObjectId('5be71ee11ad34410df06852c') }
以 createdDate 为空记录 (Long version)
2018-11-11 02:07:28.858 INFO 4310 --- [on(6)-127.0.0.1] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:183}] to localhost:27017
Person [id=null, name=Joe, email=aa@aa.aa, createdOn=null, modifiedOn=null, version=null]
2018-11-11 02:07:31.519 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.auditing.AuditingHandler : Touched Person [id=null, name=Joe, email=aa@aa.aa, createdOn=null, modifiedOn=2018-11-11T02:07:31.518, version=0] - Last modification at 2018-11-11T02:07:31.518 by unknown
2018-11-11 02:07:31.525 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [modifiedOn, version, name, email, _class] in collection: person
2018-11-11 02:07:31.564 DEBUG 4310 --- [nio-8080-exec-1] o.s.d.m.r.query.MongoQueryCreator : Created query Query: { "name" : "Joe" }, Fields: { }, Sort: { }
2018-11-11 02:07:31.571 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : find using query: { "name" : "Joe" } fields: Document{{}} for class: class com.seedu.server.model.Person in collection: person
Person [id=5be71e631ad34410d6a3b123, name=Joe, email=aa@aa.aa, createdOn=null, modifiedOn=2018-11-11T02:07:31.518, version=0]
2018-11-11 02:07:31.590 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.auditing.AuditingHandler : Touched Person [id=5be71e631ad34410d6a3b123, name=Joe, email=bb@bb.bb, createdOn=null, modifiedOn=2018-11-11T02:07:31.590, version=1] - Last modification at 2018-11-11T02:07:31.590 by unknown
2018-11-11 02:07:31.598 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : Calling update using query: { "_id" : { "$oid" : "5be71e631ad34410d6a3b123" }, "version" : { "$numberLong" : "0" } } and update: { "modifiedOn" : { "$date" : 1541873251590 }, "version" : { "$numberLong" : "1" }, "name" : "Joe", "email" : "bb@bb.bb", "_class" : "com.seedu.server.model.Person" } in collection: person
2018-11-11 02:07:31.602 DEBUG 4310 --- [nio-8080-exec-1] o.s.d.m.r.query.MongoQueryCreator : Created query Query: { "name" : "Joe" }, Fields: { }, Sort: { }
2018-11-11 02:07:31.602 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : find using query: { "name" : "Joe" } fields: Document{{}} for class: class com.seedu.server.model.Person in collection: person
2018-11-11 02:07:31.603 DEBUG 4310 --- [nio-8080-exec-1] o.s.d.m.r.query.MongoQueryCreator : Created query Query: { "email" : "bb@bb.bb" }, Fields: { }, Sort: { }
2018-11-11 02:07:31.604 DEBUG 4310 --- [nio-8080-exec-1] o.s.data.mongodb.core.MongoTemplate : find using query: { "email" : "bb@bb.bb" } fields: Document{{}} for class: class com.seedu.server.model.Person in collection: person
Person [id=5be71e631ad34410d6a3b123, name=Joe, email=bb@bb.bb, createdOn=null, modifiedOn=2018-11-11T02:07:31.590, version=1]
Person [id=5be71e631ad34410d6a3b123, name=Joe, email=bb@bb.bb, createdOn=null, modifiedOn=2018-11-11T02:07:31.590, version=1]
据我所知,spring 使用 id 存在作为保存行为控制,这意味着如果 id 存在,那么保存就像 mongo 的插入一样。但是,在这里,版本似乎也会影响保存行为或影响spring识别id存在的方式。
问题:如何将 MongoAudit 与 MongoRepository 一起使用?我犯了什么错误/错误吗?
【问题讨论】:
-
而且,没有
@Version,一切正常。我的 Spring-Boot 版本是 2.1.0.RELEASE,Java 版本是 1.8 -
我正要问一个类似的问题!我们正在从 Spring Boot 1.5.x 升级到 Spring Boot 2.1.0,并且行为发生了变化。之前工作的相同代码(版本为长)现在不设置 createdDate
标签: java spring mongodb spring-boot spring-data