Fix @MongoId mapping for insertAll.
This commit fixes an issue where id properties annotated with MongoId had not been converted into the desired target type when inserting a collection of objects instead a single one. Resolves: #4944 Original pull request: #4945
This commit is contained in:
committed by
Mark Paluch
parent
0e481b2ac1
commit
37f2d39e9e
@@ -1414,7 +1414,10 @@ public class MongoTemplate
|
||||
maybeEmitEvent(new BeforeSaveEvent<>(initialized, document, collectionName));
|
||||
initialized = maybeCallBeforeSave(initialized, document, collectionName);
|
||||
|
||||
documentList.add(document);
|
||||
MappedDocument mappedDocument = queryOperations.createInsertContext(MappedDocument.of(document))
|
||||
.prepareId(uninitialized.getClass());
|
||||
|
||||
documentList.add(mappedDocument.getDocument());
|
||||
initializedBatchToSave.add(initialized);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -3109,6 +3110,18 @@ public class MongoTemplateTests {
|
||||
assertThat(jesse.getId()).isNotNull();
|
||||
}
|
||||
|
||||
@Test // GH-4944
|
||||
public void insertAllShouldConvertIdToTargetTypeBeforeSave() {
|
||||
|
||||
RawStringId walter = new RawStringId();
|
||||
walter.value = "walter";
|
||||
|
||||
RawStringId returned = template.insertAll(List.of(walter)).iterator().next();
|
||||
org.bson.Document document = template.execute(RawStringId.class, collection -> collection.find().first());
|
||||
|
||||
assertThat(returned.id).isEqualTo(document.get("_id"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1208
|
||||
public void takesSortIntoAccountWhenStreaming() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user