Browse Source

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.
pull/12/head
Oliver Gierke 13 years ago
parent
commit
d882af257f
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

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

13
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -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;

Loading…
Cancel
Save