Skip to content

Commit 478396c

Browse files
committed
DATAMONGO-675 - MongoTemplate now maps Update in findAndModify(…).
The Update object handed to ….findAndModify(…) is now handed through the QueryMapper before being executed.
1 parent aa80d1a commit 478396c

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
13351335
/**
13361336
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. The object is
13371337
* converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless configured
1338-
* otherwise, an instance of MappingMongoConverter will be used. The query document is specified as a standard DBObject
1339-
* and so is the fields specification. Can be overridden by subclasses.
1338+
* otherwise, an instance of MappingMongoConverter will be used. The query document is specified as a standard
1339+
* DBObject and so is the fields specification. Can be overridden by subclasses.
13401340
*
13411341
* @param collectionName name of the collection to retrieve the objects from
13421342
* @param query the query document that specifies the criteria used to find a record
@@ -1437,19 +1437,15 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
14371437

14381438
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
14391439

1440-
DBObject updateObj = update.getUpdateObject();
1441-
for (String key : updateObj.keySet()) {
1442-
updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
1443-
}
1444-
1440+
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(), entity);
14451441
DBObject mappedQuery = mapper.getMappedObject(query, entity);
14461442

14471443
if (LOGGER.isDebugEnabled()) {
14481444
LOGGER.debug("findAndModify using query: " + mappedQuery + " fields: " + fields + " sort: " + sort
1449-
+ " for class: " + entityClass + " and update: " + updateObj + " in collection: " + collectionName);
1445+
+ " for class: " + entityClass + " and update: " + mappedUpdate + " in collection: " + collectionName);
14501446
}
14511447

1452-
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, updateObj, options),
1448+
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
14531449
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
14541450
}
14551451

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,23 @@ public DBObject getUpdateObject() {
217217
return dbo;
218218
}
219219

220-
@SuppressWarnings("unchecked")
221220
protected void addMultiFieldOperation(String operator, String key, Object value) {
221+
222222
Object existingValue = this.modifierOps.get(operator);
223-
LinkedHashMap<String, Object> keyValueMap;
223+
DBObject keyValueMap;
224+
224225
if (existingValue == null) {
225-
keyValueMap = new LinkedHashMap<String, Object>();
226+
keyValueMap = new BasicDBObject();
226227
this.modifierOps.put(operator, keyValueMap);
227228
} else {
228-
if (existingValue instanceof LinkedHashMap) {
229-
keyValueMap = (LinkedHashMap<String, Object>) existingValue;
229+
if (existingValue instanceof BasicDBObject) {
230+
keyValueMap = (BasicDBObject) existingValue;
230231
} else {
231232
throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was "
232233
+ existingValue.getClass());
233234
}
234235
}
236+
235237
keyValueMap.put(key, value);
236238
}
237-
238239
}

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.data.mongodb.core.index.Index.Duplicates;
6262
import org.springframework.data.mongodb.core.index.IndexField;
6363
import org.springframework.data.mongodb.core.index.IndexInfo;
64+
import org.springframework.data.mongodb.core.mapping.Field;
6465
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
6566
import org.springframework.data.mongodb.core.query.BasicQuery;
6667
import org.springframework.data.mongodb.core.query.Criteria;
@@ -92,15 +93,12 @@
9293
@ContextConfiguration("classpath:infrastructure.xml")
9394
public class MongoTemplateTests {
9495

95-
@Autowired
96-
MongoTemplate template;
97-
@Autowired
98-
MongoDbFactory factory;
96+
@Autowired MongoTemplate template;
97+
@Autowired MongoDbFactory factory;
9998

10099
MongoTemplate mappingTemplate;
101100

102-
@Rule
103-
public ExpectedException thrown = ExpectedException.none();
101+
@Rule public ExpectedException thrown = ExpectedException.none();
104102

105103
@Autowired
106104
@SuppressWarnings("unchecked")
@@ -150,6 +148,7 @@ protected void cleanDb() {
150148
template.dropCollection(TestClass.class);
151149
template.dropCollection(Sample.class);
152150
template.dropCollection(MyPerson.class);
151+
template.dropCollection(TypeWithFieldAnnotation.class);
153152
template.dropCollection("collection");
154153
template.dropCollection("personX");
155154
}
@@ -772,8 +771,7 @@ public void testUsingInQueryWithList() throws Exception {
772771
Query q3 = new Query(Criteria.where("age").in(l1, l2));
773772
template.find(q3, PersonWithIdPropertyOfTypeObjectId.class);
774773
Assert.fail("Should have trown an InvalidDocumentStoreApiUsageException");
775-
} catch (InvalidMongoDbApiUsageException e) {
776-
}
774+
} catch (InvalidMongoDbApiUsageException e) {}
777775
}
778776

779777
@Test
@@ -1625,6 +1623,25 @@ public void executesExistsCorrectly() {
16251623
assertThat(template.exists(query, Sample.class, template.getCollectionName(Sample.class)), is(true));
16261624
}
16271625

1626+
/**
1627+
* @see DATAMONGO-675
1628+
*/
1629+
@Test
1630+
public void updateConsidersMappingAnnotations() {
1631+
1632+
TypeWithFieldAnnotation entity = new TypeWithFieldAnnotation();
1633+
entity.emailAddress = "old";
1634+
1635+
template.save(entity);
1636+
1637+
Query query = query(where("_id").is(entity.id));
1638+
Update update = Update.update("emailAddress", "new");
1639+
1640+
FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);
1641+
TypeWithFieldAnnotation result = template.findAndModify(query, update, options, TypeWithFieldAnnotation.class);
1642+
assertThat(result.emailAddress, is("new"));
1643+
}
1644+
16281645
static class MyId {
16291646

16301647
String first;
@@ -1633,14 +1650,12 @@ static class MyId {
16331650

16341651
static class TypeWithMyId {
16351652

1636-
@Id
1637-
MyId id;
1653+
@Id MyId id;
16381654
}
16391655

16401656
public static class Sample {
16411657

1642-
@Id
1643-
String id;
1658+
@Id String id;
16441659
String field;
16451660
}
16461661

@@ -1697,8 +1712,13 @@ static class Address {
16971712

16981713
static class VersionedPerson {
16991714

1700-
@Version
1701-
Long version;
1715+
@Version Long version;
17021716
String id, firstname, lastname;
17031717
}
1718+
1719+
static class TypeWithFieldAnnotation {
1720+
1721+
@Id ObjectId id;
1722+
@Field("email") String emailAddress;
1723+
}
17041724
}

0 commit comments

Comments
 (0)