use*_*717 3 hibernate oracle11g java-8 jpa-2.1
无法使用@Temporal
for编译代码LocalDate
。
实体代码
...
@Temporal(TemporalType.DATE)
private LocalDate creationDate;
public LocalDate getCreationDate() {
return this.creationDate;
}
public void setCreationDate(LocalDate creationDate) {
this.creationDate = creationDate;
}
...
Run Code Online (Sandbox Code Playgroud)
转换器代码
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
持久化文件
...
<persistence-unit name="HR">
<class>test.Employee</class>
<class>test.DateConverter</class>
</persistence-unit>
...
Run Code Online (Sandbox Code Playgroud)
环境
Eclipse 编译错误(在@Temporal
注释行,在实体代码中):
临时类型的持久字段或属性必须是 java.util.Date、java.util.Calendar 或 java.util.GregorianCalendar 类型
删除@Temporal
编译很好。java.time
Java8 中的日期和时间类( ) 不是必需的吗?
请帮忙解决问题,谢谢。
col*_*ict 12
在@Temporal
不需要新的注释java.time
类包装。需要它java.util.Date
,因为那个“日期”是对一种通用的解决方案的尝试,这只会使事情变得更糟。
你也不需要转换器。
归档时间: |
|
查看次数: |
4332 次 |
最近记录: |