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
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConversionContext.java
+2-6
Original file line number
Diff line number
Diff line change
@@ -50,11 +50,7 @@ public <T> T write(@Nullable Object value, TypeInformation<T> target) {
50
50
51
51
@Override
52
52
public <T> Tread(@NullableObjectvalue, TypeInformation<T> target) {
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mongo-property-converters.adoc
+33-27
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
[[mongo.property-converters]]
2
2
== Property Converters - Mapping specific fields
3
3
4
-
Although to the <<mongo.custom-converters, typebased conversion>> already offers means to influence the representation of certain types within the target store it has its limitations when not all potential values of that type should be considered as a conversion targets.
5
-
Propertybased converters allow to specify conversion instructions on a perproperty basis either declarative, via `@ValueConverter`, or programmatic by registering a `PropertyValueConverter` for a specific field.
4
+
While <<mongo.custom-converters, type-based conversion>> already offers ways to influence the conversion and representation of certain types within the target store it has its limitations when only certain values or properties of a particular type should be considered for conversion.
5
+
Property-based converters allow configuring conversion rules on a per-property basis, either declarative, via `@ValueConverter`, or programmatic by registering a `PropertyValueConverter` for a specific property.
6
6
7
-
A `PropertyValueConverter` is responsible of transforming a given value into its store representation (write) and back (read) as shown in the snippet below.
8
-
Please mind the presence of the `ValueConversionContext` providing additional information, such as mapping metadata.
7
+
A `PropertyValueConverter` can transform a given value into its store representation (**write**) and back (**read**) as shown in the snippet below.
8
+
The additional `ValueConversionContext` provides additional information, such as mapping metadata and direct `read`/`write` methods.
9
9
10
-
.PropertyValueConverter
10
+
.A simple PropertyValueConverter
11
11
====
12
12
[source,java]
13
13
----
@@ -26,69 +26,75 @@ class ReversingValueConverter implements PropertyValueConverter<String, String,
26
26
----
27
27
====
28
28
29
-
`PropertyValueConverter` instances can be obtained via `CustomConversions#getPropertyValueConverter(...)` delegating to `PropertyValueConversions` typically using a `PropertyValueConverterFactory` to provide the actual converter.
30
-
Depending on the applications needs multiple instances of `PropertyValueConverterFactory` can be chained or decorated (eg. for caching).
31
-
By default a caching implementation is used that is capable of serving types with a default constructor or enum values.
32
-
A set of predefined factories is available via `PropertyValueConverterFactory`.
33
-
To obtain a `PropertyValueConverter` from an `ApplicationContext` make sure to use the `PropertyValueConverterFactory.beanFactoryAware(...)` factory.
29
+
`PropertyValueConverter` instances can be obtained via `CustomConversions#getPropertyValueConverter(…)` delegating to `PropertyValueConversions`, typically using a `PropertyValueConverterFactory` providing the actual converter.
30
+
Depending on the applications needs, multiple instances of `PropertyValueConverterFactory` can be chained or decorated, for example to apply caching.
31
+
By default, a caching implementation is used that is capable of serving types with a default constructor or enum values.
32
+
A set of predefined factories is available through `PropertyValueConverterFactory` factory methods.
33
+
Use `PropertyValueConverterFactory.beanFactoryAware(…)` to obtain a `PropertyValueConverter` instances from an `ApplicationContext`.
34
34
35
-
Changing the default behavior can be done via the `ConverterConfiguration`.
35
+
You can change the default behavior through `ConverterConfiguration`.
36
36
37
+
[[mongo.property-converters.declarative]]
37
38
=== Declarative Value Converter
38
39
39
-
The most straight forward usage of a `PropertyValueConverter` is via the `@ValueConverter` annotation referring to the target converter type.
40
+
The most straight forward usage of a `PropertyValueConverter` is by annotating properties with the `@ValueConverter` annotation that defines the converter type.
40
41
41
42
.Declarative PropertyValueConverter
42
43
====
43
44
[source,java]
44
45
----
45
-
public class Person {
46
-
// ...
46
+
class Person {
47
+
47
48
@ValueConverter(ReversingValueConverter.class)
48
49
String ssn;
49
50
}
50
51
----
51
52
====
52
53
53
-
=== Programmatic Value Converter
54
+
[[mongo.property-converters.programmatic]]
55
+
=== Programmatic Value Converter Registration
54
56
55
-
Following the programmatic approach does not require to put additional annotations on the domain model but registers `PropertyValueConverter` instances for certain paths in a `PropertyValueConverterRegistrar` as shown below.
57
+
Programmatic registration registers `PropertyValueConverter` instances for properties within an entity model using a `PropertyValueConverterRegistrar` as shown below.
58
+
The difference to declarative registration is that programmatic registration happens entirely outside of the entity model.
59
+
Such an approach is useful if you cannot or do not want to annotate the entity model.
56
60
57
-
.Programmatic PropertyValueConverter
61
+
.Programmatic PropertyValueConverter registration
58
62
====
59
63
[source,java]
60
64
----
61
65
PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar();
62
66
63
-
registrar.registerConverter(Address.class, "street", new PropertyValueConverter() { ... }); <1>
67
+
registrar.registerConverter(Address.class, "street", new PropertyValueConverter() { … }); <1>
<1> Register a converter for the field identified by its name.
71
76
<2> Type safe variant that allows to register a converter and its conversion functions.
72
77
====
73
78
74
79
[WARNING]
75
80
====
76
-
Dotnotation (eg. `registerConverter(Person.class, "address.street", ...)`) is *not* supported when registering converters.
81
+
Dot-notation (such as `registerConverter(Person.class, "address.street", …)`) nagivating across properties into subdocuments is *not* supported when registering converters.
77
82
====
78
83
84
+
[[mongo.property-converters.value-conversions]]
79
85
=== MongoDB property value conversions
80
86
81
87
The above sections outlined the purpose an overall structure of `PropertyValueConverters`.
82
88
This section will focus on MongoDB specific aspects.
83
89
84
-
==== MongoValueConverter & MongoConversionContext
90
+
==== MongoValueConverter and MongoConversionContext
85
91
86
-
The `MongoValueConverter` offers a pre typed `PropertyValueConverter` interface leveraging the `MongoConversionContext`.
92
+
`MongoValueConverter` offers a pre typed `PropertyValueConverter` interface leveraging the `MongoConversionContext`.
87
93
88
94
==== MongoCustomConversions configuration
89
95
90
-
`MongoCustomConversions` are by default capable of dealing with declarative value converters depending on the configured `PropertyValueConverterFactory`.
91
-
The `MongoConverterConfigurationAdapter` is there to help set up programmatic value conversions or define the `PropertyValueConverterFactory` to be used.
96
+
`MongoCustomConversions` are by default capable of handling declarative value converters depending on the configured `PropertyValueConverterFactory`.
97
+
`MongoConverterConfigurationAdapter` is there to help set up programmatic value conversions or define the `PropertyValueConverterFactory` to be used.
92
98
93
99
.Configuration Sample
94
100
====
@@ -97,10 +103,10 @@ The `MongoConverterConfigurationAdapter` is there to help set up programmatic va
0 commit comments