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
.
DATE Data TypeFor each DATE value, Oracle Database stores the following information: century, year, month, date, hour, minute, and second.
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.
@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.
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.
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 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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With