From d882af257fbf0c04f364b049b291a490ad7dd96f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 17 Sep 2012 11:57:09 +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 | 2 +- .../data/mongodb/core/MongoTemplateTests.java | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 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 462847101..2bd93cf1d 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 @@ -963,7 +963,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 b3336b306..ff30aa8ef 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 @@ -1313,12 +1313,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 { @@ -1350,6 +1351,12 @@ public class MongoTemplateTests { } } + static class PersonWithConvertedId { + + String id; + String name; + } + static enum DateTimeToDateConverter implements Converter { INSTANCE;