Reputation: 3377
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.
Upvotes: 2
Views: 1308
Reputation: 3502
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();
}
}
Upvotes: 0
Reputation: 3377
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.
Upvotes: 4