@Converter annotated class not automatically detected in spring-boot project

I am using spring-boot 1.2.2 with hibernate.version: 4.3.6. Initially, for a simple operation, I used @Converter to map the java8 LocalDateTime field to a timestamp.

In my converter class, I used autoApply = true, as shown 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 @Convert for my object. The converter class is part of the scanned packets. Is this something I have to do to get this to work automatically without using @Convert for all the records in the database?

:: Optional ::

Here is my DB configuration

 @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; } 
+7
spring spring-boot type-conversion hibernate
source share
2 answers

The only thing I see is perhaps to change this line below

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

Consequently, Spring will know how to automatically convert attribute types.

+5
source share

The order is wrong, it should be:

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

According to Javadoc:

 javax.persistence.AttributeConverter<X, Y> Parameters: X the type of the entity attribute Y the type of the database column 
+4
source share

All Articles