From bf89cce43cabd950186dcada705951f13dd6c3cb Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 17 Sep 2012 11:44:30 +0200 Subject: [PATCH] DATAMONGO-539 - Fixed MongoTemplate.remove(object, collectionName). If the entity being removed using MongoTemplate.remove(object, collectionName) contained an id that could be converted into an ObjectID it wasn't removed correctly currently. This was caused by the fact that the intermediate call didn't hand over the entity type and thus the id conversion failed. This in turn caused the query not to match the previous saved object. --- .../data/mongodb/core/MongoTemplate.java | 4 ++-- .../data/mongodb/core/MongoTemplateTests.java | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 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 055590447..22b7ef043 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 @@ -456,7 +456,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } else { query.limit(1); List results = find(query, entityClass, collectionName); - return (results.isEmpty() ? null : results.get(0)); + return results.isEmpty() ? null : results.get(0); } } @@ -879,7 +879,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return; } - remove(getIdQueryFor(object), collection); + doRemove(collection, getIdQueryFor(object), object.getClass()); } /** 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 64d4ea31b..fd18323e8 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 @@ -1235,12 +1235,13 @@ public class MongoTemplateTests { String collectionName = "explicit"; template.remove(new Query(), collectionName); - Person person = new Person("Dave"); + PersonWithConvertedId person = new PersonWithConvertedId(); + person.name = "Dave"; template.save(person, collectionName); - assertThat(template.findAll(Person.class, collectionName).isEmpty(), is(false)); + assertThat(template.findAll(PersonWithConvertedId.class, collectionName).isEmpty(), is(false)); template.remove(person, collectionName); - assertThat(template.findAll(Person.class, collectionName).isEmpty(), is(true)); + assertThat(template.findAll(PersonWithConvertedId.class, collectionName).isEmpty(), is(true)); } static class MyId { @@ -1272,6 +1273,12 @@ public class MongoTemplateTests { } } + static class PersonWithConvertedId { + + String id; + String name; + } + static enum DateTimeToDateConverter implements Converter { INSTANCE;