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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With