Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Oracle Date.

like image 357
Marcus Leon Avatar asked Feb 03 '10 00:02

Marcus Leon


People also ask

Does Oracle date type include time?

DATE Data TypeFor each DATE value, Oracle Database stores the following information: century, year, month, date, hour, minute, and second.

Does Java SQL date include time?

Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero.

What is @temporal TemporalType timestamp?

@Temporal is a JPA annotation that converts back and forth between timestamp and java. util. Date . It also converts time-stamp into time. For example, in the snippet below, @Temporal(TemporalType.

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


4 Answers

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

like image 173
Brian Deterling Avatar answered Sep 26 '22 23:09

Brian Deterling


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.

like image 44
Pascal Thivent Avatar answered Sep 25 '22 23:09

Pascal Thivent


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

like image 31
Andrzej Doyle Avatar answered Sep 24 '22 23:09

Andrzej Doyle


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

like image 42
wallenborn Avatar answered Sep 24 '22 23:09

wallenborn