sql >> Base de Datos >  >> RDS >> Mysql

Spring Data JPA con Java 8 LocalDateTime

De acuerdo con JPA 2.1, LocalDateTime no es compatible oficialmente (probablemente en poco tiempo, JPA 2.2 será oficial). Soporte de Hibernate 5 como 'versión anticipada'

Portátil y compatible desde JPA 2.0 es javax.persistence.AttributeConverter , funciona muy bien en todos los proveedores de JPA (y no hace nada malo en Hibernate 5)

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {

@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
    return (locDate == null ? null : Date.valueOf(locDate));
}

@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
    return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}