Browse Source

Polishing.

Adopt rebase changes. Update documentation to reflect default changes.

Original pull request: #4957
See: #4920
pull/5026/head
Mark Paluch 5 months ago
parent
commit
73ce59533a
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java
  2. 15
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/BigDecimalToStringConvertingTemplateTests.java
  3. 15
      src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc
  4. 8
      src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java

@ -337,7 +337,7 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
* @since 3.4 * @since 3.4
* @deprecated since 4.2. Use {@link #withPropertyValueConversions(PropertyValueConversions)} instead. * @deprecated since 4.2. Use {@link #withPropertyValueConversions(PropertyValueConversions)} instead.
*/ */
@Deprecated(since = "4.2.0") @Deprecated(since = "4.2")
public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) { public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) {
return withPropertyValueConversions(valueConversions); return withPropertyValueConversions(valueConversions);
} }
@ -457,15 +457,17 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
public enum BigDecimalRepresentation { public enum BigDecimalRepresentation {
/** /**
* @deprecated since 5.0.0 storing values as {@link Number#toString() String} retains precision, * @deprecated since 5.0. Storing values as {@link Number#toString() String} retains precision, but prevents
* but prevents efficient range queries. Prefer {@link #DECIMAL128} for better query support. * efficient range queries. Prefer {@link #DECIMAL128} for better query support.
*/ */
@Deprecated(since = "5.0.0") @Deprecated(since = "5.0")
STRING, STRING,
/** /**
* Store numbers using {@link org.bson.types.Decimal128}. Requires MongoDB Server 3.4 or later. * Store numbers using {@link org.bson.types.Decimal128} (default). Requires MongoDB Server 3.4 or later.
*/ */
DECIMAL128 DECIMAL128
} }
} }

15
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/BigDecimalToStringConvertingTemplateTests.java

@ -15,9 +15,9 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.query; import static org.springframework.data.mongodb.core.query.Query.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -25,7 +25,7 @@ import java.util.List;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.data.auditing.IsNewAwareAuditingHandler; import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.mapping.context.PersistentEntities; import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.mongodb.core.MongoTemplateTests.TypeWithNumbers; import org.springframework.data.mongodb.core.MongoTemplateTests.TypeWithNumbers;
@ -35,7 +35,6 @@ import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.test.util.Client; import org.springframework.data.mongodb.test.util.Client;
import org.springframework.data.mongodb.test.util.MongoClientExtension;
import org.springframework.data.mongodb.test.util.MongoTestTemplate; import org.springframework.data.mongodb.test.util.MongoTestTemplate;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
@ -45,7 +44,6 @@ import com.mongodb.client.MongoClient;
* *
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@ExtendWith(MongoClientExtension.class)
public class BigDecimalToStringConvertingTemplateTests { public class BigDecimalToStringConvertingTemplateTests {
public static final String DB_NAME = "mongo-template-tests"; public static final String DB_NAME = "mongo-template-tests";
@ -77,7 +75,7 @@ public class BigDecimalToStringConvertingTemplateTests {
}); });
@AfterEach @AfterEach
public void cleanUp() { void cleanUp() {
template.flush(); template.flush();
} }
@ -157,8 +155,7 @@ public class BigDecimalToStringConvertingTemplateTests {
Update update = new Update()// Update update = new Update()//
.min("bigIntegerVal", new BigInteger("700")) // .min("bigIntegerVal", new BigInteger("700")) //
.min("bigDeciamVal", new BigDecimal("800")) // .min("bigDeciamVal", new BigDecimal("800"));
;
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class); template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);

15
src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc

@ -14,7 +14,7 @@ public class Payment {
@Id String id; <1> @Id String id; <1>
@Field(targetType = FieldType.DECIMAL128) <2> @Field(targetType = FieldType.STRING) <2>
BigDecimal value; BigDecimal value;
Date date; <3> Date date; <3>
@ -26,15 +26,15 @@ public class Payment {
---- ----
{ {
"_id" : ObjectId("5ca4a34fa264a01503b36af8"), <1> "_id" : ObjectId("5ca4a34fa264a01503b36af8"), <1>
"value" : NumberDecimal(2.099), <2> "value" : "2.099", <2>
"date" : ISODate("2019-04-03T12:11:01.870Z") <3> "date" : ISODate("2019-04-03T12:11:01.870Z") <3>
} }
---- ----
<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] <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]
for details. for details.
<2> The desired target type is explicitly defined as `Decimal128` which translates to `NumberDecimal`. <2> The desired target type is explicitly defined as `String`.
Otherwise, the Otherwise, the
`BigDecimal` value would have been turned into a `String`. `BigDecimal` value would have been turned into a `Decimal128`.
<3> `Date` values are handled by the MongoDB driver itself are stored as `ISODate`. <3> `Date` values are handled by the MongoDB driver itself are stored as `ISODate`.
==== ====
@ -111,11 +111,12 @@ class MyMongoConfiguration extends AbstractMongoClientConfiguration {
MongoDB in its early days did not have support for large numeric values such as `BigDecimal`. MongoDB in its early days did not have support for large numeric values such as `BigDecimal`.
To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted values their `String` representation. To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted values their `String` representation.
This approach had several downsides due to lexical instead of numeric comparison for queries, updates, etc. This approach had several downsides due to lexical instead of numeric comparison for queries, updates, etc.
With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`. With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`.
As off Spring Data MongoDB 5.0 the default representation of those types moved to MongoDB native `org.bson.types.Decimal128` as well. As of Spring Data MongoDB 5.0. the default representation of those types moved to MongoDB native `org.bson.types.Decimal128`.
You can still use the to the deprecated `String` variant by configuring the big decimal representation in `MongoCustomConversions` through `MongoCustomConversions.create(config -> config.bigDecimal(BigDecimalRepresentation.STRING))`. 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))`.
[NOTE] [NOTE]
==== ====
Very large values, though being valid in their java, might exceed the maximum bit length of `org.bson.types.Decimal128` in their store native representation. Very large values, though being a valid `BigDecimal` or `BigInteger`, might exceed the maximum bit length of `org.bson.types.Decimal128` in their store native representation.
==== ====

8
src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc

@ -546,7 +546,7 @@ Like for `BigDecimal`, which is represented as `String` instead of `Decimal128`,
---- ----
public class Balance { public class Balance {
@Field(targetType = DECIMAL128) @Field(targetType = STRING)
private BigDecimal value; private BigDecimal value;
// ... // ...
@ -559,14 +559,14 @@ You may even consider your own, custom annotation.
---- ----
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Field(targetType = FieldType.DECIMAL128) @Field(targetType = FieldType.STRING)
public @interface Decimal128 { } public @interface AsString { }
// ... // ...
public class Balance { public class Balance {
@Decimal128 @AsString
private BigDecimal value; private BigDecimal value;
// ... // ...

Loading…
Cancel
Save