Permalink
Browse files

DATAMONGO-622 - Saving unversioned object now uses doInsert(…).

We now rather use doInsert(…) if a versioned object is saved the first time to prevent accidental updates not bumping the version number.
  • Loading branch information...
1 parent e13208b commit e56a8597b8652411df9ee0df57e73c8d76bcd98f @olivergierke olivergierke committed Feb 26, 2013
@@ -771,8 +771,7 @@ public void save(Object objectToSave, String collectionName) {
// Fresh instance -> initialize version property
if (version == null) {
- beanWrapper.setProperty(versionProperty, 0);
- doSave(collectionName, objectToSave, this.mongoConverter);
+ doInsert(collectionName, objectToSave, this.mongoConverter);
} else {
assertUpdateableIdIfNotSet(objectToSave);
@@ -42,6 +42,7 @@
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.annotation.Id;
@@ -1498,6 +1499,24 @@ public void correctlySetsLongVersionProperty() {
assertThat(person.version, is(0L));
}
+ /**
+ * @see DATAMONGO-622
+ */
+ @Test(expected = DuplicateKeyException.class)
+ public void preventsDuplicateInsert() {
+
+ template.setWriteConcern(WriteConcern.SAFE);
+
+ PersonWithVersionPropertyOfTypeInteger person = new PersonWithVersionPropertyOfTypeInteger();
+ person.firstName = "Dave";
+
+ template.save(person);
+ assertThat(person.version, is(0));
+
+ person.version = null;
+ template.save(person);
+ }
+
static class MyId {
String first;

0 comments on commit e56a859

Please sign in to comment.