9

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;
}
Varesh
  • 1,648
  • 2
  • 14
  • 22
  • And `@Converter` is what? Spring only detects `@Component` annotated beans (or beans annotated iwth a annotation that itself is annotated with `@Component` like `@Service`.). – M. Deinum Mar 26 '15 at 15:25
  • Agreed @M.Deinum but I am configuring hibernate also and has defined the packages for Hibernate to scan. I have also updated the question with my Database and Hibernate Config class. I am guessing, I am missing something... I am not sure what it is? – Varesh Mar 28 '15 at 22:04
  • You are using Spring Boot why configure everything by hand. But again what is `@Converter` if it isn't a `@Component` it doesn't do anything. Also hibernate probably needs to know about it instead of spring. – M. Deinum Mar 29 '15 at 14:01
  • I agree, but I don't know how to make hibernate aware of this converter. Is there a way to tell hibernate to embed this converter to the list of converters it already has..? – Varesh Mar 30 '15 at 07:07
  • 2
    You can use `@EntityScan` and give it a list of packages to scan for Hibernate. You can include the package where your JPA entities are as well as the package for the `Converter`. Then there is no need for the custom configuration anymore (unless you require it). – SteveO Jul 16 '15 at 16:07

2 Answers2

5

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.

erolkaya84
  • 1,769
  • 21
  • 28
4

The order is incorrect, it should be:

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

As the Javadoc states:

javax.persistence.AttributeConverter<X, Y>
Parameters:
  X the type of the entity attribute
  Y the type of the database column
Jean-Christophe
  • 624
  • 5
  • 11