无法使用@Temporal为LocalDate编译代码。
实体代码
...
@Temporal(TemporalType.DATE)
private LocalDate creationDate;
public LocalDate getCreationDate() {
return this.creationDate;
}
public void setCreationDate(LocalDate creationDate) {
this.creationDate = creationDate;
}
...转换器码
@Converter(autoApply=true)
public class DateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate localDate) {
return (localDate == null) ? null : Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
@Override
public LocalDate convertToEntityAttribute(Date date) {
return (date==null) ? null : Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
}persistence.xml
...
<persistence-unit name="HR">
<class>test.Employee</class>
<class>test.DateConverter</class>
</persistence-unit>
...环境
Eclipse编译错误(在@Temporal注释行中,在实体代码中):
时态类型的持久字段或属性必须为java.util.Date、java.util.Calendar或java.util.GregorianCalendar类型。
删除@Temporal可以很好地编译。它不是java.time中的日期和时间类( Java8 )所必需的吗?
请帮忙解决这个问题,谢谢。
发布于 2019-01-08 18:49:02
新的类的@Temporal包不需要java.time注释。这是java.util.Date需要的,因为那个“日期”是一种一刀切的解决方案,只会让事情变得更糟。
你也不需要转换器。
https://stackoverflow.com/questions/54097100
复制相似问题