Skip to content

Fix MongoId mapping for insertAll. #4945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.0-GH-4944-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.0-GH-4944-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.0-GH-4944-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Original file line number Diff line number Diff line change
@@ -1433,7 +1433,10 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
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);
}

Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
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;
@@ -3110,6 +3111,18 @@ public void generatesIdForInsertAll() {
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() {