Tôi có một ứng dụng SpringBoot. sử dụng Spring Initializer, nhúng Tomcat, công cụ mẫu Thymeleaf và gói dưới dạng tệp JAR thực thi.LocalDateTime, ZonedDateTime và Timestamp
Tôi có đối tượng miền với 2 thuộc tính (initDate, endDate). Tôi muốn tạo ra 2 bộ chuyển đổi để đối phó với mySQL DB
@Convert(converter = LocalDateTimeAttributeConverter.class)
private LocalDateTime initDate;
@Convert(converter = ZonedDateTimeAttributeConverter.class)
private ZonedDateTime endDate;
bộ chuyển đổi 1 (là OK)
@Converter
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
}
}
Đây là một trong đó tôi muốn tạo
@Converter
public class ZonedDateTimeAttributeConverter implements AttributeConverter<ZonedDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(ZonedDateTime zoneDateTime) {
return (zoneDateTime == null ? null : Timestamp.valueOf(zoneDateTime));
}
@Override
public ZonedDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null ? null : sqlTimestamp.toZonedDateTime());
}
}
Nhưng tôi không thể vì tôi có 2 lỗi:
The method valueOf(String) in the type Timestamp is not applicable for the arguments (ZonedDateTime)
và các dấu thời gian không có phương pháp toZonedDateTime()
và nếu tôi không thêm bất kỳ chuyển đổi cho ZonedDate, JPA tạo ra một bảng với kiểu varbinary(255)
Bạn muốn sử dụng múi giờ nào? (Nó có vẻ khá bị hỏng hơn bạn có thể chuyển đổi một 'Dấu thời gian' thành một' LocalDateTime', nhưng chúng ta đi.) Bạn cũng có thể muốn đi qua 'Timestamp.toInstant()' và 'Timestamp.from (Instant)'. –