You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make sure big number representation allows for backwards compatibility.
Extend configuration options in a way that allows to register different conversion options for numeric values so users can recreate the 4.x default behaviour using String as default while honoring field specific configuration via the targetType attribute of the Field Annotation.
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -155,7 +155,7 @@ public static class MongoConverterConfigurationAdapter {
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-4.x-to-5.x.adoc
+4-1Lines changed: 4 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ XML::
40
40
== BigInteger/BigDecimal Conversion Changes
41
41
42
42
Spring Data no longer defaults BigInteger/BigDecimal conversion via its configuration support classes.
43
-
In order to persist those values the `BigDecimalRepresentation` hast to be set explicitly.
43
+
In order to persist those values the default `BigDecimalRepresentation` hast to be set explicitly.
44
44
45
45
[source,java]
46
46
----
@@ -55,3 +55,6 @@ static class Config extends AbstractMongoClientConfiguration {
55
55
// ...
56
56
}
57
57
----
58
+
59
+
Users upgrading from prior versions may choose `BigDecimalRepresentation.STRING` as default.
60
+
Those using`@Field(targetType = FieldType.DECIMAL128)` need to define a combination of representations `configAdapter.bigDecimal(BigDecimalRepresentation.STRING, BigDecimalRepresentation.DECIMAL128)` to set defaulting to String while having the `DECIMAL128` converter being registered for usage with explicit target type configuration.
The most trivial way of influencing the mapping result is by specifying the desired native MongoDB target type via the `@Field` annotation.
7
-
This allows to work with non MongoDB types like `BigDecimal` in the domain model while persisting values in native `org.bson.types.Decimal128` format.
7
+
This allows to work with non MongoDB types like `BigDecimal` in the domain model while persisting values in eg. `String` format.
8
8
9
9
.Explicit target type mapping
10
10
====
@@ -33,8 +33,7 @@ public class Payment {
33
33
<1> String _id_ values that represent a valid `ObjectId` are converted automatically. See xref:mongodb/template-crud-operations.adoc#mongo-template.id-handling[How the `_id` Field is Handled in the Mapping Layer]
34
34
for details.
35
35
<2> The desired target type is explicitly defined as `String`.
36
-
Otherwise, the
37
-
`BigDecimal` value would have been turned into a `Decimal128`.
36
+
Otherwise.
38
37
<3> `Date` values are handled by the MongoDB driver itself are stored as `ISODate`.
39
38
====
40
39
@@ -113,8 +112,10 @@ To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted v
113
112
This approach had several downsides due to lexical instead of numeric comparison for queries, updates, etc.
114
113
115
114
With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`.
116
-
As of Spring Data MongoDB 5.0. the default representation of those types moved to MongoDB native `org.bson.types.Decimal128`.
117
-
You can still use the to the previous `String` variant by configuring the big decimal representation in `MongoCustomConversions` through `MongoCustomConversions.create(config -> config.bigDecimal(BigDecimalRepresentation.STRING))`.
115
+
As of Spring Data MongoDB 5.0. there no longer is a default representation of those types and conversion needs to be configured explicitly.
116
+
You can register multiple formats, 1st being default, and still retain the previous behaviour by configuring the `BigDecimalRepresentation` in `MongoCustomConversions` through `MongoCustomConversions.create(config -> config.bigDecimal(BigDecimalRepresentation.STRING, BigDecimalRepresentation.DECIMAL128))`.
117
+
This allows you to make use of the explicit storage type format via `@Field(targetType = DECIMAL128)` while keeping default conversion set to String.
118
+
Choosing none of the provided representations is valid as long as those values are no persisted.
0 commit comments