Skip to content

Make JdbcCustomConversions more configurable [DATAJDBC-648] #865

Open
@spring-projects-issues

Description

@spring-projects-issues

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)

Metadata

Metadata

Assignees

Labels

in: mappingMapping and conversion infrastructuretype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions