From fd6e4000b52283bafb5625a192d144ccc1c97957 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Mon, 6 Jan 2014 11:55:54 +0100 Subject: [PATCH] DATAMONGO-816 - Improve query handling in MongoTemplate.executeQuery(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now process the given query with the queryMapper before passing it on to the executeQueryInternal(…) in order to deal with potentially required query modifications, e.g. enum value conversions. Original pull request: #108. --- .../data/mongodb/core/MongoTemplate.java | 4 +- .../data/mongodb/core/MongoTemplateTests.java | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 656020c12..2facec8c3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -369,7 +369,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { Assert.notNull(query); - DBObject queryObject = query.getQueryObject(); + DBObject queryObject = queryMapper.getMappedObject(query.getQueryObject(), null); DBObject sortObject = query.getSortObject(); DBObject fieldsObject = query.getFieldsObject(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 38410ce82..1d37d3b4b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,6 +72,7 @@ import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.StringUtils; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; @@ -163,6 +164,7 @@ public class MongoTemplateTests { template.dropCollection(ObjectWith3AliasedFields.class); template.dropCollection(ObjectWith3AliasedFieldsAndNestedAddress.class); template.dropCollection(BaseDoc.class); + template.dropCollection(ObjectWithEnumValue.class); } @Test @@ -2058,6 +2060,33 @@ public class MongoTemplateTests { assertThat(result.get(0).field, is(value)); } + /** + * @see DATAMONGO-816 + */ + @Test + public void shouldExecuteQueryShouldMapQueryBeforeQueryExecution() { + + ObjectWithEnumValue o = new ObjectWithEnumValue(); + o.value = EnumValue.VALUE2; + template.save(o); + + Query q = Query.query(Criteria.where("value").in(EnumValue.VALUE2)); + + template.executeQuery(q, StringUtils.uncapitalize(ObjectWithEnumValue.class.getSimpleName()), + new DocumentCallbackHandler() { + + @Override + public void processDocument(DBObject dbObject) throws MongoException, DataAccessException { + + assertThat(dbObject, is(notNullValue())); + + ObjectWithEnumValue result = template.getConverter().read(ObjectWithEnumValue.class, dbObject); + + assertThat(result.value, is(EnumValue.VALUE2)); + } + }); + } + static interface Model { String value(); @@ -2183,4 +2212,14 @@ public class MongoTemplateTests { static class ObjectWith3AliasedFieldsAndNestedAddress extends ObjectWith3AliasedFields { @Field("adr") Address address; } + + static enum EnumValue { + VALUE1, VALUE2, VALUE3 + } + + static class ObjectWithEnumValue { + + @Id String id; + EnumValue value; + } }