开发者

JPA 2.0 Oracle DATE has null time

开发者 https://www.devze.com 2023-01-11 12:47 出处:网络
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 i

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.

I thought before I posted any c开发者_高级运维ode, 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;

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

column READING_DATE. Found: date, expected: timestamp

Any/all replies are appreciated.


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

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;

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):

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.

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>

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

3.3. JDBC connections

...

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.

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).

Related issue

  • HHH-1566


Use ojdbc6.jar and use the below code against hibernate 3.3 set timestamp instead of java.sql.date or java.util.date

@Id
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TDATE", length = 7)
public Date getTdate() {
    return this.tdate;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消