Browse Source

DATAJDBC-287 - Polishing.

Moved the converter into it's own enum demonstrating good practices.
Added `@ReadingConverter`.

Original pull request: #106.
pull/108/head
Jens Schauder 7 years ago
parent
commit
44e164fda4
  1. 26
      src/main/asciidoc/jdbc.adoc

26
src/main/asciidoc/jdbc.adoc

@ -145,27 +145,45 @@ This also means references are 1-1 or 1-n, but not n-1 or n-m.
If you have n-1 or n-m references, you are, by definition, dealing with two separate aggregates. If you have n-1 or n-m references, you are, by definition, dealing with two separate aggregates.
References between those should be encoded as simple `id` values, which should map properly with Spring Data JDBC. References between those should be encoded as simple `id` values, which should map properly with Spring Data JDBC.
[[jdbc.entity-persistence.custom-converters]]
=== Custom converters === Custom converters
Custom coverters can be registered, for types that are not supported by default, by inheriting your configuration from `JdbcConfiguration` and overwriting the method `jdbcCustomConversions()`.
Custom converters can be registered, for types that are not supported by default, by inheriting your configuration from `JdbcConfiguration` and overwriting the method `jdbcCustomConversions()`.
==== ====
[source, java] [source, java]
---- ----
@Configuration @Configuration
public class DataJdbcConfiguration extends JdbcConfiguration { public class DataJdbcConfiguration extends JdbcConfiguration {
@Override @Override
protected JdbcCustomConversions jdbcCustomConversions() { protected JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(Arrays.asList(new Converter<TIMESTAMPTZ, Date>() {
return new JdbcCustomConversions(Collections.singletonList(TimestampTzToDateConverter.INSTANCE));
}
@ReadingConverter
enum TimestampTzToDateConverter implements Converter<TIMESTAMPTZ, Date> {
INSTANCE;
@Override @Override
public Date convert(TIMESTAMPTZ source) { public Date convert(TIMESTAMPTZ source) {
//... //...
} }
}));
} }
} }
---- ----
==== ====
The constructor of `JdbcCustomConversions` accepts a list of `org.springframework.core.convert.converter.Converter`. 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.
[[jdbc.entity-persistence.naming-strategy]] [[jdbc.entity-persistence.naming-strategy]]
=== `NamingStrategy` === `NamingStrategy`

Loading…
Cancel
Save