Skip to content

Preserve given Id on insert. #4203

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 3 commits into from
Closed
Show file tree
Hide file tree
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
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4184-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4184-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4184-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1326,9 +1326,10 @@ default long exactCount(Query query, String collectionName) {
/**
* Insert the object into the collection for the entity type of the object to save. <br />
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. <br />
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
* If your object has an {@literal Id} property which holds a {@literal null} value, it will be set with the generated
* Id from MongoDB. If your Id property is a String then MongoDB ObjectId will be used to populate that string.
* Otherwise, the conversion from ObjectId to your property type will be handled by Spring's BeanWrapper class that
* leverages Type Conversion API. See
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
* Type Conversion"</a> for more details. <br />
* Insert is used to initially store the object into the database. To update an existing object use the save method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ <T> MappedDocument prepareId(Class<T> type) {
*/
<T> MappedDocument prepareId(@Nullable MongoPersistentEntity<T> entity) {

if (entity == null) {
if (entity == null || source.hasId()) {
return source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,10 @@ default Mono<Long> exactCount(Query query, String collectionName) {
/**
* Insert the object into the collection for the entity type of the object to save. <br />
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. <br />
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
* If your object has an {@literal Id} property which holds a {@literal null} value, it will be set with the generated
* Id from MongoDB. If your Id property is a String then MongoDB ObjectId will be used to populate that string.
* Otherwise, the conversion from ObjectId to your property type will be handled by Spring's BeanWrapper class that
* leverages Type Conversion API. See
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
* Type Conversion"</a> for more details. <br />
* Insert is used to initially store the object into the database. To update an existing object use the save method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3636,6 +3636,26 @@ public void saveAndLoadStringThatIsAnObjectIdAsString() {
assertThat(target).isEqualTo(source);
}

@Test // GH-4184
void insertHonorsExistingRawId() {

RawStringId source = new RawStringId();
source.id = "abc";
source.value = "new value";

template.insert(source);

org.bson.Document result = template
.execute(db -> db.getCollection(template.getCollectionName(RawStringId.class))
.find().limit(1).cursor().next());

assertThat(result).isNotNull();
assertThat(result.get("_id")).isEqualTo("abc");

RawStringId target = template.findOne(query(where("id").is(source.id)), RawStringId.class);
assertThat(target).isEqualTo(source);
}

@Test // GH-4026
void saveShouldGenerateNewIdOfTypeIfExplicitlyDefined() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ void insertContextDoesNotAddConvertedIdForMongoIdTypesTargetingObjectId() {
});
}

@Test // GH-4184
void insertContextDoesNotOverrideExistingId() {

assertThat(queryOperations.createInsertContext(new Document("_id", "abc")).prepareId(Person.class).getDocument())//
.satisfies(result -> {
assertThat(result).isEqualTo(new Document("_id", "abc"));
});
}

static class Person {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ void insertShouldGenerateNewIdOfTypeIfExplicitlyDefined() {
}).verifyComplete();
}

@Test // GH-4184
void insertHonorsExistingRawId() {

MongoTemplateTests.RawStringId source = new MongoTemplateTests.RawStringId();
source.id = "abc";
source.value = "new value";

template.insert(source)
.then(template.execute(db -> Flux.from(
db.getCollection(template.getCollectionName(MongoTemplateTests.RawStringId.class)).find().limit(1).first()))
.next())
.as(StepVerifier::create).consumeNextWith(result -> {
assertThat(result).isNotNull();
assertThat(result.get("_id")).isEqualTo("abc");
});
}

@Test // DATAMONGO-1444
void insertsSimpleEntityCorrectly() {

Expand Down