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.
This commit is contained in:
Oliver Gierke
2012-09-17 11:57:09 +02:00
parent c92058a79a
commit d882af257f
2 changed files with 11 additions and 4 deletions

View File

@@ -963,7 +963,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
return; return;
} }
remove(getIdQueryFor(object), collection); doRemove(collection, getIdQueryFor(object), object.getClass());
} }
/** /**

View File

@@ -1313,12 +1313,13 @@ public class MongoTemplateTests {
String collectionName = "explicit"; String collectionName = "explicit";
template.remove(new Query(), collectionName); template.remove(new Query(), collectionName);
Person person = new Person("Dave"); PersonWithConvertedId person = new PersonWithConvertedId();
person.name = "Dave";
template.save(person, collectionName); 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); 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 { static class MyId {
@@ -1350,6 +1351,12 @@ public class MongoTemplateTests {
} }
} }
static class PersonWithConvertedId {
String id;
String name;
}
static enum DateTimeToDateConverter implements Converter<DateTime, Date> { static enum DateTimeToDateConverter implements Converter<DateTime, Date> {
INSTANCE; INSTANCE;