DATAMONGO-617 - Fixed potential NullPointerException in MongoTemplate.insert(…).

If MongoTemplate.insert(…) was called with a Mongo-simple type (such as a raw DBBobject) it caused a NullPointerException during the lookup of a version property. This is now fixed by correcting the guard.
This commit is contained in:
Oliver Gierke
2013-02-20 12:04:24 +01:00
parent 7bcf142c8d
commit a2136719e1
2 changed files with 13 additions and 1 deletions

View File

@@ -666,7 +666,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoPersistentEntity<?> mongoPersistentEntity = getPersistentEntity(entity.getClass());
if (mongoPersistentEntity == null || mongoPersistentEntity.hasVersionProperty()) {
if (mongoPersistentEntity != null && mongoPersistentEntity.hasVersionProperty()) {
BeanWrapper<PersistentEntity<Object, ?>, Object> wrapper = BeanWrapper.create(entity, null);
wrapper.setProperty(mongoPersistentEntity.getVersionProperty(), 0);
}

View File

@@ -1318,6 +1318,18 @@ public class MongoTemplateTests {
template.save(person);
}
/**
* @see DATAMONGO-617
*/
@Test
public void doesNotFailOnVersionInitForUnversionedEntity() {
DBObject dbObject = new BasicDBObject();
dbObject.put("firstName", "Oliver");
template.insert(dbObject, template.determineCollectionName(PersonWithVersionPropertyOfTypeInteger.class));
}
/**
* @see DATAMONGO-539
*/