Browse Source

DATADOC-230 - Added method to remove objects from specific collection.

Added MongoOperations.remove(Object, String) and according MongoTemplate implementation to be able to explicitly define the collection an object should be removed from. This aligns to the method signatures we provide for all other methods as well.

Consolidated MongoTemplate.getIdPropertName(…) and ….getIdValue(…) into ….getIdQueryFor(…) as they we're only used inside a method building a by-id-query for an object.
pull/1/head
Oliver Gierke 14 years ago
parent
commit
717ff38319
  1. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
  2. 74
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 17
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

10
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); 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 * @param object
*/ */
void remove(Object 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 * Remove all documents that match the provided query document criteria from the the collection used to store the

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

@ -809,7 +809,50 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
return; 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 <T> void remove(Query query, Class<T> entityClass) { public <T> void remove(Query query, Class<T> entityClass) {
@ -1135,35 +1178,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName); new ReadDbObjectCallback<T>(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. * Populates the id property of the saved object, if it's not set already.
* *

17
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.size(), is(1));
assertThat(testClassList.get(0).getMyDate(), is(testClass.getMyDate())); 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 { public class TestClass {

Loading…
Cancel
Save