DATADOC-166 - Check for null on removing objects.

This commit is contained in:
Oliver Gierke
2011-07-14 21:34:11 +02:00
parent 58b9db28a8
commit f424b7c760
2 changed files with 21 additions and 3 deletions

View File

@@ -1004,10 +1004,18 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
return wr;
}
});
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#remove(java.lang.Object)
*/
public void remove(Object object) {
if (object == null) {
return;
}
remove(new Query(where(getIdPropertyName(object))
.is(getIdValue(object))), object.getClass());
}
@@ -1301,9 +1309,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
protected String getIdPropertyName(Object object) {
Assert.notNull(object);
MongoPersistentEntity<?> persistentEntity = mappingContext
.getPersistentEntity(object.getClass());
MongoPersistentProperty idProperty = persistentEntity.getIdProperty();
return idProperty == null ? ID : idProperty.getName();
}

View File

@@ -83,8 +83,8 @@ public class MongoTemplateTests {
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class,
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class,
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class,
PersonWithIdPropertyOfPrimitiveLong.class)));
mappingContext.afterPropertiesSet();
@@ -758,4 +758,11 @@ public class MongoTemplateTests {
});
}
/**
* @see DATADOC-166
*/
@Test
public void removingNullIsANoOp() {
template.remove(null);
}
}