gpt4 book ai didi

java - 如何在 JPA (eclipselink) 中使用 joda 时间?

转载 作者:太空狗 更新时间:2023-10-29 23:03:01 26 4
gpt4 key购买 nike

我试图在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com