使用 MongoTemplate 我正在尝试存储一个名为 Person 的实体。由于我想使用乐观锁定,因此使用 @Version 注释对实体进行版本控制。
当我尝试使用 MongoTemplate.save 将新的 Person 实体存储到空集合中时,我得到 OptimisticLockingException。我没想到会这样,因为我正在创建一个新对象而不是更新现有对象。(并且没有其他线程正在访问该集合。)
这是预期的行为,还是我做错了什么?
(如果我改用 MongoOperations.insert,一切正常。(但我想使用 save,因为 CrudRepository 只有保存,没有更新。)如果我删除 @Version 注释,它也可以工作。)
谢谢,丹尼尔
我的实体类:
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Person {
@Version
private long versionId;
@Id
private ObjectId id;
private final String name;
public Person(String name) {
this.name = name;
}
public long getVersionId() {
return versionId;
}
public ObjectId getId() {
return id;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (id != null ? !id.equals(person.id) : person.id != null) return false;
return true;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
我的测试设置(使用 EmbedMongo 设置 MongoDb 实例):
import com.mongodb.Mongo;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
public class MongoDbTest {
private MongodExecutable mongodExe;
private MongodProcess mongod;
private Mongo mongo;
private MongoOperations mongoOperations;
@Before
public void setUp() throws Exception {
MongodStarter runtime = MongodStarter.getDefaultInstance();
mongodExe = runtime.prepare(new MongodConfig(Version.Main.V2_0, 12345, Network.localhostIsIPv6()));
mongod = mongodExe.start();
mongo = new Mongo("localhost", 12345);
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, "database");
MappingContext mappingContext = new MongoMappingContext();
MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(mongoDbFactory, mappingContext);
mongoOperations = new MongoTemplate(mongoDbFactory, mappingMongoConverter);
}
@After
public void tearDown() {
mongod.stop();
mongodExe.stop();
}
@Test
public void testSave() {
Person person = new Person("Joe");
mongoOperations.save(person); // This call throws OptimisticLockingException.
}
}