spring-projects / spring-projects/spring-data-relational

Make JdbcCustomConversions more configurable [DATAJDBC-648]

Open
#865 0 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Dec 31, 2020.

in: mapping type: enhancement
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.