diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index a58e7e3c1..d30c6504f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -657,11 +657,19 @@ public interface MongoOperations { WriteResult updateMulti(Query query, Update update, String collectionName); /** - * Remove the given object from the collection by Id + * Remove the given object from the collection by id. * * @param object */ void remove(Object object); + + /** + * Removes the given object from the given collection. + * + * @param object + * @param collection must not be {@literal null} or empty. + */ + void remove(Object object, String collection); /** * Remove all documents that match the provided query document criteria from the the collection used to store the 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 c7d787297..d5dec787a 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 @@ -809,7 +809,50 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return; } - remove(new Query(where(getIdPropertyName(object)).is(getIdValue(object))), object.getClass()); + remove(getIdQueryFor(object), object.getClass()); + } + + public void remove(Object object, String collection) { + + Assert.hasText(collection); + + if (object == null) { + return; + } + + remove(getIdQueryFor(object), collection); + } + + /** + * Returns a {@link Query} for the given entity by its id. + * + * @param object must not be {@literal null}. + * @return + */ + private Query getIdQueryFor(Object object) { + + Assert.notNull(object); + + MongoPersistentEntity entity = mappingContext.getPersistentEntity(object.getClass()); + MongoPersistentProperty idProp = entity.getIdProperty(); + + if (idProp == null) { + throw new MappingException("No id property found for object of type " + entity.getType().getName()); + } + + ConversionService service = mongoConverter.getConversionService(); + Object idProperty = null; + + try { + + idProperty = BeanWrapper.create(object, service).getProperty(idProp, Object.class, true); + return new Query(where(idProp.getFieldName()).is(idProperty)); + + } catch (IllegalAccessException e) { + throw new MappingException(e.getMessage(), e); + } catch (InvocationTargetException e) { + throw new MappingException(e.getMessage(), e); + } } public void remove(Query query, Class entityClass) { @@ -1135,35 +1178,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { new ReadDbObjectCallback(readerToUse, entityClass), collectionName); } - protected Object getIdValue(Object object) { - - MongoPersistentEntity entity = mappingContext.getPersistentEntity(object.getClass()); - MongoPersistentProperty idProp = entity.getIdProperty(); - - if (idProp == null) { - throw new MappingException("No id property found for object of type " + entity.getType().getName()); - } - - ConversionService service = mongoConverter.getConversionService(); - - try { - return BeanWrapper.create(object, service).getProperty(idProp, Object.class, true); - } catch (IllegalAccessException e) { - throw new MappingException(e.getMessage(), e); - } catch (InvocationTargetException e) { - throw new MappingException(e.getMessage(), e); - } - } - - protected String getIdPropertyName(Object object) { - Assert.notNull(object); - - MongoPersistentEntity persistentEntity = mappingContext.getPersistentEntity(object.getClass()); - MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); - - return idProperty == null ? ID : idProperty.getName(); - } - /** * Populates the id property of the saved object, if it's not set already. * 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 fbe2419fd..01466d579 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 @@ -914,6 +914,23 @@ public class MongoTemplateTests { assertThat(testClassList.size(), is(1)); assertThat(testClassList.get(0).getMyDate(), is(testClass.getMyDate())); } + + /** + * @see DATADOC-230 + */ + @Test + public void removesEntityFromCollection() { + + template.remove(new Query(), "mycollection"); + + Person person = new Person("Dave"); + + template.save(person, "mycollection"); + assertThat(template.findAll(TestClass.class, "mycollection").size(), is(1)); + + template.remove(person, "mycollection"); + assertThat(template.findAll(Person.class, "mycollection").isEmpty(), is(true)); + } public class TestClass {