Generate and convert id on insert if explicitly defined.
We now make sure to provide an id value that matches the desired target type when no id is set, and the property defines an explicit conversion target.
Previously a new ObjectId would have been generated which leads to type inconsistencies when querying for _id.
Closes#4026
Original pull request: #4057.
3.4.x
Christoph Strobl4 years agocommitted byMark Paluch
@ -1543,18 +1543,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@@ -1543,18 +1543,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@ -1605,7 +1608,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@@ -1605,7 +1608,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@ -1686,7 +1686,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@@ -1686,7 +1686,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
.format("Inserting Document containing fields: "+dbDoc.keySet()+" in collection: "+collectionName));
@ -1696,10 +1697,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@@ -1696,10 +1697,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@ -1763,7 +1764,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@@ -1763,7 +1764,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@ -58,8 +58,8 @@ The following outlines what field will be mapped to the `_id` document field:
@@ -58,8 +58,8 @@ The following outlines what field will be mapped to the `_id` document field:
The following outlines what type conversion, if any, will be done on the property mapped to the _id document field.
* If a field named `id` is declared as a String or BigInteger in the Java class it will be converted to and stored as an ObjectId if possible. ObjectId as a field type is also valid. If you specify a value for `id` in your application, the conversion to an ObjectId is detected to the MongoDB driver. If the specified `id` value cannot be converted to an ObjectId, then the value will be stored as is in the document's _id field. This also applies if the field is annotated with `@Id`.
* If a field is annotated with `@MongoId` in the Java class it will be converted to and stored as using its actual type. No further conversion happens unless `@MongoId` declares a desired field type.
* If a field is annotated with `@MongoId(FieldType.…)` in the Java class it will be attempted to convert the value to the declared `FieldType.`
* If a field is annotated with `@MongoId` in the Java class it will be converted to and stored as using its actual type. No further conversion happens unless `@MongoId` declares a desired field type. If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the properties type.
* If a field is annotated with `@MongoId(FieldType.…)` in the Java class it will be attempted to convert the value to the declared `FieldType`. If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the declared type.
* If a field named `id` id field is not declared as a String, BigInteger, or ObjectID in the Java class then you should assign it a value in your application so it can be stored 'as-is' in the document's _id field.
* If no field named `id` is present in the Java class then an implicit `_id` file will be generated by the driver but not mapped to a property or field of the Java class.