JPA 2.0 Oracle DATE has null time

前端 未结 2 1588
太阳男子
太阳男子 2021-02-10 01:31

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 in

相关标签:
2条回答
  • 2021-02-10 01:50

    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
    0 讨论(0)
  • 2021-02-10 01:51

    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 讨论(0)
提交回复
热议问题