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:
@@ -938,13 +938,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
|||||||
for (T o : batchToSave) {
|
for (T o : batchToSave) {
|
||||||
|
|
||||||
initializeVersionProperty(o);
|
initializeVersionProperty(o);
|
||||||
Document dbDoc = new Document();
|
|
||||||
|
|
||||||
maybeEmitEvent(new BeforeConvertEvent<T>(o, collectionName));
|
maybeEmitEvent(new BeforeConvertEvent<T>(o, collectionName));
|
||||||
writer.write(o, dbDoc);
|
Document document = toDocument(o, writer);
|
||||||
|
|
||||||
maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc, collectionName));
|
maybeEmitEvent(new BeforeSaveEvent<T>(o, document, collectionName));
|
||||||
documentList.add(dbDoc);
|
documentList.add(document);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Object> ids = consolidateIdentifiers(insertDocumentList(collectionName, documentList), documentList);
|
List<Object> ids = consolidateIdentifiers(insertDocumentList(collectionName, documentList), documentList);
|
||||||
|
|||||||
@@ -3026,6 +3026,24 @@ public class MongoTemplateTests {
|
|||||||
assertThat(result, hasItems(newYork, washington));
|
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
|
* @see DATAMONGO-1208
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user