我试图在我的 entity
类中使用 DataTime
。在字段上方添加 @Temporal(TemporalType.DATE)
,我收到错误提示 "The persistent field or property for a Temporal type must be of type java.util.Date, java.util .Calendar 或 java.util.GregorianCalendar”。我可以来回介绍转换;使用 setter 和 getter 如下:
@Temporal(TemporalType.DATE)
private Calendar attendanceDate;
public DateTime getAttendanceDate() {
return new DateTime(this.attendanceDate);
}
public void setAttendanceDate(DateTime attendanceDate) {
this.attendanceDate = attendanceDate.toCalendar(Locale.getDefault());
}
但我希望eclipselink
为我做这件事。我已经穿过'Persist Joda-time's DateTime via Hibernate .答案建议使用hibernate,但我必须使用eclipselink。我可以在我的实体类中使用 DateTime
对象,DB 表示形式为 BLOB
,但我需要它作为 Date
。有没有类似jodatime-eclipselink
的东西?或者还有什么建议?感谢您的帮助。
最佳答案
基本链接定义了一个 EclipseLink 转换器,用于将 Joda DateTime 转换为 java.sql.Timestamp 或 Date。 您可以使用它,或者定义您自己的转换器并通过 EclipseLink 中的@Convert、@Converter 使用它。
对于 DDL 创建,转换器应定义初始化方法并将映射字段上的类型设置为 java.sql.Timestamp。
请在 EclipseLink 中记录错误(或为现有错误投票),我们应该支持 Joda。
关于java - 如何在 JPA (eclipselink) 中使用 joda 时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9783577/