Spring Data JDBC allows registration of custom converters to influence how values are mapped in the database.
@ -55,12 +57,26 @@ class MyJdbcConfiguration extends AbstractJdbcConfiguration {
@@ -55,12 +57,26 @@ class MyJdbcConfiguration extends AbstractJdbcConfiguration {
// …
@Overwrite
@Bean
public JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(Arrays.asList(new BooleanToStringConverter(), new StringToBooleanConverter()));
}
@Override
protected List<?> userConverters() {
return Arrays.asList(new BooleanToStringConverter(), new StringToBooleanConverter());
}
}
----
NOTE: In previous versions of Spring Data JDBC it was recommended to directly overwrite `AbstractJdbcConfiguration.jdbcCustomConversions()`.
This is no longer necessary or even recommended, since that method assembles conversions intended for all databases, conversions registered by the `Dialect` used and conversions registered by the user.
If you are migrating from an older version of Spring Data JDBC and have `AbstractJdbcConfiguration.jdbcCustomConversions()` overwritten conversions from your `Dialect` will not get registered.
Custom converters can be registered, for types that are not supported by default, by inheriting your configuration from `AbstractJdbcConfiguration` and overwriting the method `jdbcCustomConversions()`.
====
[source,java]
----
@Configuration
class DataJdbcConfiguration extends AbstractJdbcConfiguration {
@Override
public JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(Collections.singletonList(TimestampTzToDateConverter.INSTANCE));
The constructor of `JdbcCustomConversions` accepts a list of `org.springframework.core.convert.converter.Converter`.
Converters should be annotated with `@ReadingConverter` or `@WritingConverter` in order to control their applicability to only reading from or to writing to the database.
`TIMESTAMPTZ` in the example is a database specific data type that needs conversion into something more suitable for a domain model.