开发者

Java type for date/time when using Oracle Date with Hibernate

开发者 https://www.devze.com 2022-12-19 03:50 出处:网络
We have a Oracle Date column.At first in our Java/Hibernate class we were using java.sql.Date.This worked but it didn\'t seem to store any time information in the database when we save so I changed th

We have a Oracle Date column. At first in our Java/Hibernate class we were using java.sql.Date. This worked but it didn't seem to store any time information in the database when we save so I changed the Java data type to Timestamp. Now we get 开发者_StackOverflow社区this error:

springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.an notation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [margin-service-domain -config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreatio nException: Error creating bean with name 'sessionFactory' defined in class path resource [m-service-doma in-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type: CREATE_TS, expected: timestamp

Any ideas on how to map an Oracle Date while retaining the time portion?


Update: I can get it to work if I use the Oracle Timestamp data type but I don't want that level of precision ideally. Just want the basic Oracle Date.


I always use java.util.Date with Oracle dates and it handles date and time just fine.


Not a direct answer but I'd use the Oracle TIMESTAMP type:

  • TIMESTAMP (fractional_seconds_ precision) Year, month, and day values of date, as well as hour, minute, and second values of time, where fractional_seconds_precision optionally specifies the number of digits in the fractional part of the SECOND datetime field and can be a number in the range 0 to 9. The default is 6. For example, you specify TIMESTAMP as a literal as follows:

    TIMESTAMP'1997-01-31 09:26:50.124'
    

with the desired fractional_second_precision.


First off - you're right that you'd need to use a java.sql.Timestamp class, as the java.sql.Date class explicitly does not represent a specific time of day (rather, it tries to represent midnight GMT).

As for your error - you haven't given enough information to do anything more than guess (it would require looking at both your Hibernate config and the class to actually determine the cause). However, if you merely changed the class of the field in the Java class, then of course you'll have to update the Hibernate mapping as well. If you haven't done the latter then this will likely lead to your mismatch. Try explicitly specifying type="timestamp" for the corresponding mapping.

EDIT: If you're using annotations, then did you update the annotation on that property to @Temporal(TemporalType.TIMESTAMP)? If you didn't, then you will need to (and if you did, you should have said so :-)).


Maybe you have this problem? Make sure you have the latest JDBC driver, the filename should be ojdbc5.jar.


Did you recreate the database after you made this change?

If Hibernate originally created the table using a java.sql.Date for the column, I'm not aware of any way it could change it based on a change in the Java code alone.


You need to use @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime") for the column defined in entity class

0

精彩评论

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

关注公众号