spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Make JdbcCustomConversions more configurable [DATAJDBC-648]

Open spring-projects-issues opened this issue 4 years ago • 0 comments
trafficstars

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

  1. Add JdbcCustomConversions(StoreConversions, Collection<?>) constructor.
  2. Change AbstractJdbcConfiguration to use CustomConversions type for "jdbcCustomConversions" bean to make possible using of custom CustomConversions instance

Affects: 2.1.2 (2020.0.2)

spring-projects-issues avatar Dec 16 '20 23:12 spring-projects-issues