Skip to content

Commit 73ce595

Browse files
committed
Polishing.
Adopt rebase changes. Update documentation to reflect default changes. Original pull request: #4957 See: #4920
1 parent c4f9369 commit 73ce595

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public MongoConverterConfigurationAdapter bigDecimal(BigDecimalRepresentation re
337337
* @since 3.4
338338
* @deprecated since 4.2. Use {@link #withPropertyValueConversions(PropertyValueConversions)} instead.
339339
*/
340-
@Deprecated(since = "4.2.0")
340+
@Deprecated(since = "4.2")
341341
public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) {
342342
return withPropertyValueConversions(valueConversions);
343343
}
@@ -457,15 +457,17 @@ private boolean hasDefaultPropertyValueConversions() {
457457
public enum BigDecimalRepresentation {
458458

459459
/**
460-
* @deprecated since 5.0.0 — storing values as {@link Number#toString() String} retains precision,
461-
* but prevents efficient range queries. Prefer {@link #DECIMAL128} for better query support.
460+
* @deprecated since 5.0. Storing values as {@link Number#toString() String} retains precision, but prevents
461+
* efficient range queries. Prefer {@link #DECIMAL128} for better query support.
462462
*/
463-
@Deprecated(since = "5.0.0")
463+
@Deprecated(since = "5.0")
464464
STRING,
465465

466466
/**
467-
* Store numbers using {@link org.bson.types.Decimal128}. Requires MongoDB Server 3.4 or later.
467+
* Store numbers using {@link org.bson.types.Decimal128} (default). Requires MongoDB Server 3.4 or later.
468468
*/
469469
DECIMAL128
470+
470471
}
472+
471473
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.springframework.data.mongodb.core.query.Criteria.where;
20-
import static org.springframework.data.mongodb.core.query.Query.query;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.query.Criteria.*;
20+
import static org.springframework.data.mongodb.core.query.Query.*;
2121

2222
import java.math.BigDecimal;
2323
import java.math.BigInteger;
2424
import java.util.List;
2525

2626
import org.junit.jupiter.api.AfterEach;
2727
import org.junit.jupiter.api.Test;
28-
import org.junit.jupiter.api.extension.ExtendWith;
28+
2929
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
3030
import org.springframework.data.mapping.context.PersistentEntities;
3131
import org.springframework.data.mongodb.core.MongoTemplateTests.TypeWithNumbers;
@@ -35,17 +35,15 @@
3535
import org.springframework.data.mongodb.core.query.Query;
3636
import org.springframework.data.mongodb.core.query.Update;
3737
import org.springframework.data.mongodb.test.util.Client;
38-
import org.springframework.data.mongodb.test.util.MongoClientExtension;
3938
import org.springframework.data.mongodb.test.util.MongoTestTemplate;
4039

4140
import com.mongodb.client.MongoClient;
4241

4342
/**
4443
* Tests for {@link MongoTemplate} using string representation of {@link BigInteger} values.
45-
*
44+
*
4645
* @author Christoph Strobl
4746
*/
48-
@ExtendWith(MongoClientExtension.class)
4947
public class BigDecimalToStringConvertingTemplateTests {
5048

5149
public static final String DB_NAME = "mongo-template-tests";
@@ -77,7 +75,7 @@ public class BigDecimalToStringConvertingTemplateTests {
7775
});
7876

7977
@AfterEach
80-
public void cleanUp() {
78+
void cleanUp() {
8179
template.flush();
8280
}
8381

@@ -157,8 +155,7 @@ void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() {
157155

158156
Update update = new Update()//
159157
.min("bigIntegerVal", new BigInteger("700")) //
160-
.min("bigDeciamVal", new BigDecimal("800")) //
161-
;
158+
.min("bigDeciamVal", new BigDecimal("800"));
162159

163160
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
164161

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Payment {
1414
1515
@Id String id; <1>
1616
17-
@Field(targetType = FieldType.DECIMAL128) <2>
17+
@Field(targetType = FieldType.STRING) <2>
1818
BigDecimal value;
1919
2020
Date date; <3>
@@ -26,15 +26,15 @@ public class Payment {
2626
----
2727
{
2828
"_id" : ObjectId("5ca4a34fa264a01503b36af8"), <1>
29-
"value" : NumberDecimal(2.099), <2>
29+
"value" : "2.099", <2>
3030
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
3131
}
3232
----
3333
<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]
3434
for details.
35-
<2> The desired target type is explicitly defined as `Decimal128` which translates to `NumberDecimal`.
35+
<2> The desired target type is explicitly defined as `String`.
3636
Otherwise, the
37-
`BigDecimal` value would have been turned into a `String`.
37+
`BigDecimal` value would have been turned into a `Decimal128`.
3838
<3> `Date` values are handled by the MongoDB driver itself are stored as `ISODate`.
3939
====
4040

@@ -111,11 +111,12 @@ class MyMongoConfiguration extends AbstractMongoClientConfiguration {
111111
MongoDB in its early days did not have support for large numeric values such as `BigDecimal`.
112112
To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted values their `String` representation.
113113
This approach had several downsides due to lexical instead of numeric comparison for queries, updates, etc.
114+
114115
With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`.
115-
As off Spring Data MongoDB 5.0 the default representation of those types moved to MongoDB native `org.bson.types.Decimal128` as well.
116-
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))`.
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))`.
117118

118119
[NOTE]
119120
====
120-
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.
121+
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.
121122
====

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ Like for `BigDecimal`, which is represented as `String` instead of `Decimal128`,
546546
----
547547
public class Balance {
548548
549-
@Field(targetType = DECIMAL128)
549+
@Field(targetType = STRING)
550550
private BigDecimal value;
551551
552552
// ...
@@ -559,14 +559,14 @@ You may even consider your own, custom annotation.
559559
----
560560
@Target(ElementType.FIELD)
561561
@Retention(RetentionPolicy.RUNTIME)
562-
@Field(targetType = FieldType.DECIMAL128)
563-
public @interface Decimal128 { }
562+
@Field(targetType = FieldType.STRING)
563+
public @interface AsString { }
564564
565565
// ...
566566
567567
public class Balance {
568568
569-
@Decimal128
569+
@AsString
570570
private BigDecimal value;
571571
572572
// ...

0 commit comments

Comments
 (0)