DATAMONGO-1176 - Exclude null id fields in bulk insert.

We no longer map _id fields into Document that are null to enforce MongoDB ObjectId generation on batch insert like insertAll.
This commit is contained in:
Mark Paluch
2016-11-17 13:56:45 +01:00
parent af4f0e0913
commit 070d784be3
2 changed files with 21 additions and 4 deletions

View File

@@ -938,13 +938,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
for (T o : batchToSave) {
initializeVersionProperty(o);
Document dbDoc = new Document();
maybeEmitEvent(new BeforeConvertEvent<T>(o, collectionName));
writer.write(o, dbDoc);
Document document = toDocument(o, writer);
maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc, collectionName));
documentList.add(dbDoc);
maybeEmitEvent(new BeforeSaveEvent<T>(o, document, collectionName));
documentList.add(document);
}
List<Object> ids = consolidateIdentifiers(insertDocumentList(collectionName, documentList), documentList);

View File

@@ -3026,6 +3026,24 @@ public class MongoTemplateTests {
assertThat(result, hasItems(newYork, washington));
}
/**
* @see DATAMONGO-1176
*/
@Test
public void generatesIdForInsertAll() {
Person walter = new Person(null, "Walter");
Person jesse = new Person(null, "Jesse");
template.insertAll(Arrays.asList(walter, jesse));
List<Person> result = template.findAll(Person.class);
assertThat(result, hasSize(2));
assertThat(walter.getId(), is(notNullValue()));
assertThat(jesse.getId(), is(notNullValue()));
}
/**
* @see DATAMONGO-1208
*/