Skip to content

Commit 6139e83

Browse files
committed
DATAMONGO-620 - Updating versioned object uses explicit collection name.
We're now handing the collection name given to doSaveVersioned(…) to the update method.
1 parent f337900 commit 6139e83

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2012 the original author or authors.
2+
* Copyright 2010-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -792,7 +792,7 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
792792
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject));
793793
Update update = Update.fromDBObject(dbObject, ID_FIELD);
794794

795-
updateFirst(query, update, objectToSave.getClass());
795+
doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
796796
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbObject));
797797
}
798798
}
@@ -941,7 +941,7 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
941941
if (entity != null && entity.hasVersionProperty() && !multi) {
942942
if (writeResult.getN() == 0) {
943943
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: "
944-
+ updateObj.toMap().toString());
944+
+ updateObj.toMap().toString() + " to collection " + collectionName);
945945
}
946946
}
947947

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,6 +146,7 @@ protected void cleanDb() {
146146
template.dropCollection(Sample.class);
147147
template.dropCollection(MyPerson.class);
148148
template.dropCollection("collection");
149+
template.dropCollection("personX");
149150
}
150151

151152
@Test
@@ -949,6 +950,7 @@ public void testFindOneWithSort() {
949950
}
950951

951952
@Test
953+
@SuppressWarnings("deprecation")
952954
public void testUsingReadPreference() throws Exception {
953955
this.template.execute("readPref", new CollectionCallback<Object>() {
954956
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
@@ -1467,6 +1469,22 @@ public void queryCantBeNull() {
14671469
assertThat(template.find(null, PersonWithIdPropertyOfTypeObjectId.class), is(result));
14681470
}
14691471

1472+
/**
1473+
* @see DATAMONGO-620
1474+
*/
1475+
@Test
1476+
public void versionsObjectIntoDedicatedCollection() {
1477+
1478+
PersonWithVersionPropertyOfTypeInteger person = new PersonWithVersionPropertyOfTypeInteger();
1479+
person.firstName = "Dave";
1480+
1481+
template.save(person, "personX");
1482+
assertThat(person.version, is(0));
1483+
1484+
template.save(person, "personX");
1485+
assertThat(person.version, is(1));
1486+
}
1487+
14701488
static class MyId {
14711489

14721490
String first;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import org.springframework.data.mongodb.core.mapping.Version;
18+
import org.springframework.data.annotation.Version;
1919

2020
public class PersonWithVersionPropertyOfTypeInteger {
2121

0 commit comments

Comments
 (0)