Java type for date/time when using Oracle Date with Hibernate
Solution 1
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 theSECOND
datetime field and can be a number in the range 0 to 9. The default is 6. For example, you specifyTIMESTAMP
as a literal as follows:TIMESTAMP'1997-01-31 09:26:50.124'
with the desired fractional_second_precision
.
Solution 2
I always use java.util.Date with Oracle dates and it handles date and time just fine.
Solution 3
Maybe you have this problem? Make sure you have the latest JDBC driver, the filename should be ojdbc5.jar.
Solution 4
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 :-)).
Marcus Leon
Director Clearing Technology, Intercontinental Exchange. Develop the clearing systems that power ICE/NYSE's derivatives markets.
Updated on January 07, 2020Comments
-
Marcus Leon over 4 years
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 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 OracleDate
. -
Marcus Leon over 14 yearsI'm using annotations, don't see the `type' field available.
-
Marcus Leon over 14 yearsTried that, same error. Wondering if it's a "Spring thing". The error references a Spring bean...
-
Marcus Leon over 14 yearsGood idea. Used that with a precision of 2. Original goal was to use Oracle
Date
but this works fine. -
Jesse Webb over 13 yearsThis is the correct answer. Pascal's answer of using a TIMESTAMP type column is just a work around. If you are 'forced' to use a DATE type column (mapping Hibernate to an existing schema which you can't modify) then you must use java.util.Date and NOT java.sql.Date. Here is a similiar question which covers the this case: stackoverflow.com/questions/960923/…
-
Jesse Webb over 13 yearsThis is just a work-around. See my comment on Brian's answer as to why his answer is the solution to your original question.
-
kmehta about 13 yearsWhen I do this, hibernate maps the column (now timestamp type) to a String instead of a Date in Java. Any ideas why?
-
Torsten Römer over 12 yearsI would not recommend this. When using java.util.Date, Hibernate maps to its TimestampType. Oracle might then cast the DATE column to TIMESTAMP so an existing index won't be used, and the performance will be really bad. Better use a timestamp all the way through.
-
dpelisek about 4 yearsSadly, the link does not work. What problem do you mean?