问题描述
我尝试在实体
类中使用 DataTime
。在字段上方添加 @Temporal(TemporalType.DATE)
,我收到错误提示Temporal类型的持久字段或属性必须是java.util类型.Date,java.util.Calendar或java.util.GregorianCalendar。我可以来回介绍转换;使用setter和getter如下:
I tried to use the DataTime
in my entity
class. Adding @Temporal(TemporalType.DATE)
above the field, I got the error saying "The persistent field or property for a Temporal type must be of type java.util.Date, java.util.Calendar or java.util.GregorianCalendar". I can introduce the conversion back and forth; using setters and getters as follows:
@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
为我做这件事。我已经离开了'。答案建议使用hibernate,但我必须使用eclipselink。我可以在我的实体类中使用 DateTime
对象,数据库表示为 BLOB
,但我需要它为日期
。有什么像 jodatime-eclipselink
?还是其他任何建议?感谢您的帮助。
but I hope eclipselink
to do it for me. I have gone thro' Persist Joda-time's DateTime via Hibernate. The answer suggesting to use hibernate, but I have to use eclipselink. I can use the DateTime
object in my entity class with DB representation as BLOB
, but I need it as Date
. Is there anything like jodatime-eclipselink
? Or any other suggestion? Thanks for the help.
推荐答案
基本链接定义了一个EclipseLink转换器,用于从Joda DateTime转换为java.sql.Timestamp或Date 。
您可以使用它,或者定义您自己的转换器并通过EclipseLink中的@Convert,@ Confverter使用它。
Basic the link defines an EclipseLink Converter to convert from Joda DateTime to java.sql.Timestamp or Date.You could use it, or define your own converter and use it through @Convert, @Converter in EclipseLink.
对于DDL创建,转换器应该定义初始化方法并将映射字段上的类型设置为java.sql.Timestamp。
For DDL creation, the converter should define the initialize method and set the type on the mapping's field to java.sql.Timestamp.
请在EclipseLink中记录此错误(或对现有错误进行投票),我们应该支持Joda。
Please log a bug (or vote for the existing one) on this in EclipseLink, we should have support for Joda.
这篇关于如何在JPA(eclipselink)中使用joda时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!