From ee43703100b620ca78be023bd49f5b171a4e0003 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 beab06007..f389ae03e 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. @@ -365,7 +365,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 3b9e56be7..937d79014 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. @@ -70,6 +70,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; @@ -160,6 +161,7 @@ public class MongoTemplateTests { template.dropCollection(ObjectWith3AliasedFields.class); template.dropCollection(ObjectWith3AliasedFieldsAndNestedAddress.class); template.dropCollection(BaseDoc.class); + template.dropCollection(ObjectWithEnumValue.class); } @Test @@ -2055,6 +2057,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(); @@ -2180,4 +2209,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; + } }