gpt4 book ai didi

jpa - EclipseLink 和 Oracle DB 出现时区错误

转载 作者:行者123 更新时间:2023-12-01 22:31:16 26 4
gpt4 key购买 nike

我正在编写一个使用 JPA 和 GlassFish 3.1.2.2 和 EclipseLink 2.3.2 的应用程序。我正在使用 Oracle DB 11g 并尝试使用 TIMESTAMPTZ 字段类型来存储带时区的日期和时间。

通过我的设置,我能够将带有时区的日期和时间保存到数据库中。 (更新-实际上,当查看实际的 SQL 调用时,它仅传递日期和时间。Oracle 在保存到数据库时必须附加时区)。

但是,当检索数据时,我收到以下异常:

Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException Exception Description: The object [oracle.sql.TIMESTAMPTZ@12cbe3f], of class [class oracle.sql.TIMESTAMPTZ], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[startDateTime-->APPT_EVENT.START_DATE_TIME]] with descriptor [RelationalDescriptor(com.ntst.caremanager.server.entities.ApptEvent --> [DatabaseTable(APPT_EVENT)])], could not be converted to [class java.util.Date].

有人遇到过这种情况吗?这就是我的实体类中的“startDateTime”字段的设置方式:

@Column(name = "START_DATE_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startDateTime;

START_DATE_TIME 在数据库中使用以下 DDL 定义:

"START_DATE_TIME" TIMESTAMP (6) WITH TIME ZONE

我已阅读 eclipselink wiki here EclipseLink 本身支持 Oracle 的 TIMESTAMPTZ,无需任何转换。我还尝试在实体类中使用“日历”类型而不是“日期”时间。

更新:也尝试过这个

@Convert("timestamptz")
@TypeConverter(name="timestamptz", dataType=TIMESTAMPTZ.class)
@Column(name = "START_DATE_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startDateTime;

还是没有运气。奇怪的是,添加转换器后,持久化到数据库不再有效。尝试保存值时出现此错误。

Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.3.v20120629-r11760): org.eclipse.persistence.exceptions.ConversionException Exception Description: The object [12/4/12 7:00 AM], of class [class java.util.Date], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[startDateTime-->APPT_EVENT.START_DATE_TIME]] with descriptor [RelationalDescriptor(com.ntst.caremanager.server.entities.ApptEvent --> [DatabaseTable(APPT_EVENT)])], could not be converted to [class oracle.sql.TIMESTAMPTZ].

尝试检索值时仍然遇到相同的错误。我还尝试将 Glassfish 更新到 EclipseLink 2.3.3 并得到了相同的错误。

这是我的持久性文件:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="CM-warPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>CMDEV</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.target-server" value="SunAS9"/>
</properties>

有人以前见过这个问题,或者看到我可能犯的任何错误吗?

谢谢!

最佳答案

您需要指定一个转换器

@Convert("timestamptz")
@TypeConverter(name="timestamptz", dataType=TIMESTAMPTZ.class)
@Column(name = "START_DATE_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startDateTime;

您还需要配置服务器平台,以便 EclipseLink 可以获得 JDBC 连接以正确进行转换。这可以在 persistence.xml 中完成

<property name="eclipselink.target-server" value="WebLogic"/>

关于jpa - EclipseLink 和 Oracle DB 出现时区错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14024565/

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