JPA 2.0 Oracle DATE有空时 [英] JPA 2.0 Oracle DATE has null time

查看:170
本文介绍了JPA 2.0 Oracle DATE有空时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个这样的骨头问题,但是我一整天都在追着我的尾巴。我有一个Struts 2 + Spring3 / JPA / Hibernate应用程序,将一个大集合插入数据库。在该集合中是一个Java实用程序日期。我检查了Dao插入行之前的日期,所有日期都有正确的时间。插入后,Oracle数据库中的所有行都没有时间,只是日期。就好像这段时间被截断,但事务中没有出现错误。

This seems like such a bonehead question, but I've been chasing my tail around a tree all day. I have a Struts 2 + Spring3/JPA/Hibernate application that inserts a large set into the DB. In the set is a Java util date. I've checked the date just before the Dao inserts the rows and all the dates have the correct times. After insert, all the rows in the Oracle DB have no time, just the date. It's as if the time is truncated, but no errors appear in the transaction.

我以前在发布任何代码之前,我会问这个问题,看看有人可以建议我可能会忽略的东西DB是Oracle 10g。 JPA 2.0。列有它的注释:

I thought before I posted any code, I would ask the question to see if someone could suggest something I may have been overlooking? The DB is Oracle 10g. JPA 2.0. The Column has the annotation on it:

@Column(name = "READING_DATE")
@Temporal(TemporalType.DATE)
private Date readingDate;

将列类型设置为TemporalType.TIMESTAMP导致Hibernate异常

Setting the Column type to TemporalType.TIMESTAMP results in a Hibernate exception

column READING_DATE. Found: date, expected: timestamp

任何/所有回复都赞赏。

Any/all replies are appreciated.

推荐答案

对于这种问题,通常有助于提供JDBC驱动程序的版本和您使用的方言。

For this kind of problems, it usually helps to provide the version of the JDBC driver and the dialect you're using.

无论如何,我的理解是,您实际需要以下映射(DATE TIME):

Anyway, my understanding is that you actually want the following mapping (for DATE and TIME):

@Column(name = "READING_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date readingDate;

您所面临的问题与Oracle 9.2版引入的疯狂有关,请参阅 DATE和TIMESTAMP发生了什么? (简而言之,他们引入了一个 TIMESTAMP 列,并更改了 DATE SQL类型的映射,请阅读常见问题)。有几个选项可以解决(我当然假设你使用的是一个 TemporalType.TIMESTAMP 映射):

And the problem you're facing is somehow related to the madness introduced by Oracle with version 9.2, see What is going on with DATE and TIMESTAMP? (in short, they introduced a TIMESTAMP column and changed the mapping of the DATE SQL type, read the FAQ). There are several options to solve it (I am of course assuming you are using a TemporalType.TIMESTAMP mapping):

使用 TIMESTAMP 列类型(我不是意味着 TemporalType.TIMESTAMP here ,我的意思是DB侧的列类型)。但是如果您不需要纳秒精度,这不是最佳选择。

Either use a TIMESTAMP column type (I don't mean TemporalType.TIMESTAMP here, I really mean the column type on the DB side). But if you don't need nanosecond precision, this is not the best choice.

或设置 oracle.jdbc.V8Compatible = true 兼容模式,作为系统属性或连接属性:

Or set the oracle.jdbc.V8Compatible=true compatibility mode, either as system property or as a connection property:

<property name="hibernate.connection.oracle.jdbc.V8Compatible">true</property>

是的,您可以设置这样的任意连接属性。从Hibernate文档:

Yes, you can set arbitrary connection properties like this. From the Hibernate documentation:


3.3。 JDBC连接



...

3.3. JDBC connections

...

任意连接属性可以通过前缀hibernate给出。连接到连接属性名称。例如,您可以使用 hibernate.connection.charSet 指定 charSet 连接属性。

Arbitrary connection properties can be given by prepending "hibernate.connection" to the connection property name. For example, you can specify a charSet connection property using hibernate.connection.charSet.

或使用Oracle JDBC 11.1驱动程序(他们通过还原更改来修复问题)。这是IMO的理想解决方案( V8Compatible 的东西已被弃用)

Or use Oracle JDBC 11.1 driver (they "fixed" the problem by reverting the change). This is IMO the ideal solution (the V8Compatible stuff is deprecated).

  • HHH-1566

这篇关于JPA 2.0 Oracle DATE有空时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆