Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Converter annotated class not getting auto detected in spring-boot project

I am using spring-boot 1.2.2 with hibernate.version:4.3.6.Final for a simple operation and was using the @Converter for mapping java8 LocalDateTime field to timestamp.

In my converter class, I used autoApply=true as below.

@Converter(autoApply = true)
public class LocalDateTimePersistenceConverter implements
    AttributeConverter {
    @Override
    public java.sql.Timestamp convertToDatabaseColumn(LocalDateTime entityValue) {
        return Timestamp.valueOf(entityValue);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(java.sql.Timestamp databaseValue) {
        return databaseValue.toLocalDateTime();
    }
}

However, I still have to use the @Convert on my entity. The converter class is part of the packages I scan. Is it something that I have to do to get this to work automatically without using the @Convert on all DB entries?

::Additionally::

Here is my DB Config

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource());
    lef.setJpaVendorAdapter(jpaVendorAdapter());
    lef.setPackagesToScan("path to domain and Converter class");
    lef.afterPropertiesSet();
    return lef;
}

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setDatabase(Database.ORACLE);
    adapter.setShowSql(false);
    adapter.setGenerateDdl(false);
    return adapter;
}
like image 566
Varesh Avatar asked Mar 26 '15 15:03

Varesh


1 Answers

The only thing I can see is you may need to change this line below

public class LocalDateTimePersistenceConverter implements
AttributeConverter<java.sql.Timestamp, LocaleDateTime>

Therefore, Spring would know how to automatically convert which type of attributes.

like image 189
erolkaya84 Avatar answered Sep 26 '22 08:09

erolkaya84