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
#411 - Introduce EnumWriteSupport for simpler pass-thru of enum values.
We now provide EnumWriteSupport as base class for enum write converters that should be written as-is to the driver.
R2dbcCustomConversions can now also be created from a dialect for easier R2dbcCustomConversions creation.
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mapping.adoc
+40-1
Original file line number
Diff line number
Diff line change
@@ -122,6 +122,10 @@ The following table explains how property types of an entity affect mapping:
122
122
|Passthru
123
123
|Can be customized using <<mapping.explicit.converters, Explicit Converters>>.
124
124
125
+
|`Enum`
126
+
|String
127
+
|Can be customized by registering a <<mapping.explicit.converters, Explicit Converters>>.
128
+
125
129
|`Blob` and `Clob`
126
130
|Passthru
127
131
|Can be customized using <<mapping.explicit.converters, Explicit Converters>>.
@@ -138,6 +142,10 @@ The following table explains how property types of an entity affect mapping:
138
142
|Array of wrapper type (e.g. `int[]` -> `Integer[]`)
139
143
|Conversion to Array type if supported by the configured <<r2dbc.drivers, driver>>, not supported otherwise.
140
144
145
+
|Driver-specific types
146
+
|Passthru
147
+
|Contributed as simple type be the used `R2dbcDialect`.
148
+
141
149
|Complex objects
142
150
|Target type depends on registered `Converter`.
143
151
|Requires a <<mapping.explicit.converters, Explicit Converters>>, not supported otherwise.
@@ -204,7 +212,7 @@ To selectively handle the conversion yourself, register one or more one or more
204
212
You can use the `r2dbcCustomConversions` method in `AbstractR2dbcConfiguration` to configure converters.
205
213
The examples <<mapping.configuration, at the beginning of this chapter>> show how to perform the configuration with Java.
206
214
207
-
NOTE: Custom top-level entity conversion requires asymmetric types for conversion.Inbound data is extracted from R2DBC's `Row`.
215
+
NOTE: Custom top-level entity conversion requires asymmetric types for conversion.Inbound data is extracted from R2DBC's `Row`.
208
216
Outbound data (to be used with `INSERT`/`UPDATE` statements) is represented as `OutboundRow` and later assembled to a statement.
209
217
210
218
The following example of a Spring Converter implementation converts from a `Row` to a `Person` POJO:
@@ -248,3 +256,34 @@ public class PersonWriteConverter implements Converter<Person, OutboundRow> {
248
256
}
249
257
----
250
258
====
259
+
260
+
[[mapping.explicit.enum.converters]]
261
+
==== Overriding Enum Mapping with Explicit Converters
262
+
263
+
Some databases, such as https://github.com/pgjdbc/r2dbc-postgresql#postgres-enum-types[Postgres], can natively write enum values using their database-specific enumerated column type.
264
+
Spring Data converts `Enum` values by default to `String` values for maximum portability.
265
+
To retain the actual enum value, register a `@Writing` converter whose source and target types use the actual enum type to avoid using `Enum.name()` conversion.
266
+
Additionally, you need to configure the enum type on the driver level so that the driver is aware how to represent the enum type.
267
+
268
+
The following example shows the involved components to read and write `Color` enum values natively:
269
+
270
+
====
271
+
[source,java]
272
+
----
273
+
enum Color {
274
+
Grey, Blue
275
+
}
276
+
277
+
class ColorConverter extends EnumWriteSupport<Color> {
0 commit comments