From 594ddbd1c1f5c0b16956e910e823f483fb704db3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Jul 2012 10:19:58 +0200 Subject: [PATCH] =?UTF-8?q?DATAMONGO-495=20-=20Fixed=20debug=20output=20in?= =?UTF-8?q?=20MongoTemplate.doFind(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using SerializationUtils to safely output the query to be executed. --- .../data/mongodb/core/MongoTemplate.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 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 d4e9d4c7d..9bed73396 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,15 +1,11 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2012 the original author or authors. * - * Licensed under t -import org.springframework.util.ResourceUtils; - -import org.springframework.data.convert.EntityReader; -he Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,6 +15,7 @@ he Apache License, Version 2.0 (the "License"); */ package org.springframework.data.mongodb.core; +import static org.springframework.data.mongodb.core.SerializationUtils.*; import static org.springframework.data.mongodb.core.query.Criteria.*; import java.io.IOException; @@ -340,7 +337,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: $s", - SerializationUtils.serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName)); + serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName)); } this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName); @@ -887,7 +884,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { /** * Returns a {@link Query} for the given entity by its id. - * + * * @param object must not be {@literal null}. * @return */ @@ -1011,8 +1008,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { handleCommandError(commandResult, commandObject); if (LOGGER.isDebugEnabled()) { - LOGGER.debug(String.format("MapReduce command result = [%s]", - SerializationUtils.serializeToJsonSafely(commandObject))); + LOGGER.debug(String.format("MapReduce command result = [%s]", serializeToJsonSafely(commandObject))); } MapReduceOutput mapReduceOutput = new MapReduceOutput(inputCollection, commandObject, commandResult); @@ -1065,8 +1061,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { DBObject commandObject = new BasicDBObject("group", dbo); if (LOGGER.isDebugEnabled()) { - LOGGER.debug(String.format("Executing Group with DBObject [%s]", - SerializationUtils.serializeToJsonSafely(commandObject))); + LOGGER.debug(String.format("Executing Group with DBObject [%s]", serializeToJsonSafely(commandObject))); } CommandResult commandResult = executeCommand(commandObject, getDb().getOptions()); @@ -1178,7 +1173,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { /** * Create the specified collection using the provided options - * + * * @param collectionName * @param collectionOptions * @return the collection that was created @@ -1200,7 +1195,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter *

* The query document is specified as a standard DBObject and so is the fields specification. - * + * * @param collectionName name of the collection to retrieve the objects from * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned @@ -1225,7 +1220,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * The query document is specified as a standard DBObject and so is the fields specification. *

* Can be overridden by subclasses. - * + * * @param collectionName name of the collection to retrieve the objects from * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned @@ -1242,11 +1237,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { protected List doFind(String collectionName, DBObject query, DBObject fields, Class entityClass, CursorPreparer preparer, DbObjectCallback objectCallback) { + MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); + if (LOGGER.isDebugEnabled()) { - LOGGER.debug("find using query: " + query + " fields: " + fields + " for class: " + entityClass - + " in collection: " + collectionName); + LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", + serializeToJsonSafely(query), fields, entityClass, collectionName)); } + return executeFindMultiInternal(new FindCallback(mapper.getMappedObject(query, entity), fields), preparer, objectCallback, collectionName); } @@ -1255,7 +1253,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter. *

* The query document is specified as a standard DBObject and so is the fields specification. - * + * * @param collectionName name of the collection to retrieve the objects from * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned @@ -1294,7 +1292,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * The first document that matches the query is returned and also removed from the collection in the database. *

* The query document is specified as a standard DBObject and so is the fields specification. - * + * * @param collectionName name of the collection to retrieve the objects from * @param query the query document that specifies the criteria used to find a record * @param entityClass the parameterized type of the returned list. @@ -1341,7 +1339,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { /** * Populates the id property of the saved object, if it's not set already. - * + * * @param savedObject * @param id */ @@ -1394,7 +1392,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { *

  • Execute the given {@link ConnectionCallback} for a {@link DBObject}.
  • *
  • Apply the given {@link DbObjectCallback} to each of the {@link DBObject}s to obtain the result.
  • *
      - * + * * @param * @param collectionCallback the callback to retrieve the {@link DBObject} with * @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type