Spring Boot 1.4: LocalDateTime mapped to varbinary instead of timestamp type

1.2k views Asked by At

I am using Java 8, Spring Boot 1.4 and hsqldb. I have an entity with java.time.LocalDateTime field. When I check sql generated by hibernate, it is using varbinary as data type. How do I make it use timestamp data type?

Update: It does work when I add hibernate-java8 (5.1.0.Final) dependency. But it does not work with hibernate-java8 (5.2.x versions). This might be because Java 8 support was added to hibernate-core 5.2 itself.

2

There are 2 answers

0
coolscitist On BEST ANSWER

It works when hibernate-java8 (5.1.0.Final) dependency is added. It DOES NOT work with hibernate-java8 (5.2.x versions). This is because Java 8 support was added to hibernate-core 5.2 itself.

0
Olivier Tonglet On

Another way would be to annotate your entity localDateTime_field with a converter.

@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime localDateTime_field;

Here is how it goes:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Timestamp;
import java.time.LocalDateTime;

@Converter
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
        return localDateTime == null ? null : Timestamp.valueOf(localDateTime);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
        return timestamp == null ? null : timestamp.toLocalDateTime();
    }
}