12

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.

2
  • Don't know what that is - so I'm guessing no. Commented Feb 3, 2010 at 16:33
  • If that is the hibernate auto generate schema functionality, then no. Commented Feb 3, 2010 at 18:24

6 Answers 6

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

This 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/…
I 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.
7

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.

3 Comments

Good idea. Used that with a precision of 2. Original goal was to use Oracle Date but this works fine.
This is just a work-around. See my comment on Brian's answer as to why his answer is the solution to your original question.
When I do this, hibernate maps the column (now timestamp type) to a String instead of a Date in Java. Any ideas why?
1

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

2 Comments

I'm using annotations, don't see the `type' field available.
Tried that, same error. Wondering if it's a "Spring thing". The error references a Spring bean...
1

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

1 Comment

Sadly, the link does not work. What problem do you mean?
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.