コンバーターを使用できます:
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Timestamp;
import java.time.LocalDateTime;
@Converter(autoApply = true)
public class DateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return localDateTime != null ? Timestamp.valueOf(localDateTime) : null;
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
return timestamp != null ? timestamp.toLocalDateTime() : null;
}
}
そして:
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Date;
import java.time.LocalDate;
@Converter(autoApply = true)
public class DateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate localDate) {
return localDate != null ? Date.valueOf(localDate) : null;
}
@Override
public LocalDate convertToEntityAttribute(Date date) {
return date != null ? date.toLocalDate() : null;
}
}