Skip to content

JdbcCustomConversions not working. Data Jdbc 3.5.6 #2183

@Tsyklop

Description

@Tsyklop

Getting exception: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.postgresql.util.PGInterval' to required type 'java.time.Duration' for property 'dueIn'; Cannot convert value of type 'org.postgresql.util.PGInterval' to required type 'java.time.Duration' for property 'dueIn': no matching editors or conversion strategy found

I tried extending from AbstractJdbcConfiguration class and override userConverters method (not working), jdbcCustomConversions method (all other onverters not working).

Spring Boot 3.5.7

What wrong?

UPDATE:
Seems like WritingConverter working - entity saved. But ReadingConverter not working - exception.

My Config:


@Configuration
@EnableJdbcAuditing
public class PersistenceConfig {

    @Bean
    public JdbcCustomConversions jdbcCustomConversions(Dialect jdbcDialect) {

        SimpleTypeHolder simpleTypeHolder = jdbcDialect.simpleTypes().isEmpty() ? JdbcSimpleTypes.HOLDER
                : new SimpleTypeHolder(jdbcDialect.simpleTypes(), JdbcSimpleTypes.HOLDER);

        List<Object> converters = new ArrayList<>();
        converters.addAll(jdbcDialect.getConverters());
        converters.addAll(JdbcCustomConversions.storeConverters());

        return new JdbcCustomConversions(
                CustomConversions.StoreConversions.of(simpleTypeHolder, converters),
                List.of(
                        new PgIntervalToDurationConverter(),
                        new DurationToPgIntervalConverter()
                )
        );
    }

}

Converters:


@ReadingConverter
public class PgIntervalToDurationConverter implements Converter<PGInterval, Duration> {
    @Override
    public Duration convert(PGInterval source) {
        if (source == null) {
            return null;
        }
        long seconds = 0;

        seconds += source.getSeconds();
        seconds += source.getMinutes() * 60L;
        seconds += source.getHours() * 3600L;
        seconds += source.getDays() * 86400L;
        seconds += source.getMonths() * 30L * 86400L;  // naive
        seconds += source.getYears() * 365L * 86400L; // naive

        return Duration.ofSeconds(seconds);
    }
}

@WritingConverter
public class DurationToPgIntervalConverter implements Converter<Duration, PGInterval> {
    @Override
    public PGInterval convert(Duration source) {
        long seconds = source.getSeconds();
        int days = (int) (seconds / (24 * 3600));
        seconds -= (long) days * 24 * 3600;
        int hours = (int) (seconds / 3600);
        seconds -= (long) hours * 3600;
        int minutes = (int) (seconds / 60);
        seconds -= (long) minutes * 60;
        double secsWithFraction = seconds + source.getNano() / 1_000_000_000.0;
        return new PGInterval(0, 0, days, hours, minutes, secsWithFraction);
    }
}

Exception:


org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.postgresql.util.PGInterval' to required type 'java.time.Duration' for property 'dueIn'; Cannot convert value of type 'org.postgresql.util.PGInterval' to required type 'java.time.Duration' for property 'dueIn': no matching editors or conversion strategy found
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:594)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:608)
	at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:455)
	at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
	at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:246)
	at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:351)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61)
	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:733)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:658)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:723)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:748)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:804)
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'org.postgresql.util.PGInterval' to required type 'java.time.Duration' for property 'dueIn': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:273)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:589)
	... 236 common frames omitted

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions