Browse Source

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.
pull/411/merge
Mark Paluch 9 years ago
parent
commit
070d784be3
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

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

@ -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);

18
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -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
*/ */

Loading…
Cancel
Save