spring-data-relational
spring-data-relational copied to clipboard
Make JdbcCustomConversions more configurable [DATAJDBC-648]
mitasov-ra opened DATAJDBC-648 and commented
The Problem
Recently I've faced problem with registering custom @ReadingConverter in JdbcConfiguration, it simply didn't work as expected. Instead of registering my CustomField as simple, Spring Data ignored converter and continued to generate "... JOIN "custom_field" ..." scripts.
The cause of this problem is in CustomConversions class, which registers customSimpleTypes only from @WritingConverter converters.
I found a solution - I can instantiate CustomConversions class instead of JdbcCustomConversions and manually specify CustomField type as simple like below:
@Bean
public CustomConversions customConversions() {
return new CustomConversions(
CustomConversions.StoreConversions.of(
new SimpleTypeHolder(
Set.of(CustomField.class),
SimpleTypeHolder.DEFAULT)),
List.of(CustomConverterReading.INSTANCE));
}
But this solution doesn't work either. The problem is in AbstractJdbcConfiguration - other beans declared there requires JdbcCustomConversions bean, not CustomConversions.
Only workaround for that is to use Reflection to modify simpleTypeHolder directly, as there's no any setters and JdbcCustomConversions doesn't override CustomConversions(StoreConversions, Collection<?>) constructor.
@SneakyThrows
@Override
@Nonnull
public JdbcCustomConversions jdbcCustomConversions() {
var bean = new JdbcCustomConversions(
List.of(
CustomConverterReading.INSTANCE));
var simpleTypeHolderField = CustomConversions.class.getDeclaredField("simpleTypeHolder");
simpleTypeHolderField.setAccessible(true);
simpleTypeHolderField.set(bean, new SimpleTypeHolder(Set.of(CustomField.class), bean.getSimpleTypeHolder()));
return bean;
}
For sure, this problem also can be solved by implementing @WritingConverter, or by writing custom JdbcConfiguration without extending AbstractJdbcConfiguration.
BUT, I think this solutions is too much for such a trivial problem.
Proposed solutions
- Add
JdbcCustomConversions(StoreConversions, Collection<?>)constructor. - Change
AbstractJdbcConfigurationto useCustomConversionstype for "jdbcCustomConversions" bean to make possible using of customCustomConversionsinstance
Affects: 2.1.2 (2020.0.2)