Storing dates in Oracle through hibernation

I am storing a simple java.util.date file in an Oracle XE database via hibernate.

When testing with JUnit, if I can get the correct value, I get an error like this:

junit.framework.AssertionFailedError: 
  expected:<Sun Dec 28 11:20:27 CET 2008> 
  but was:<2008-12-28 11:20:27.0>

      

The value is stored in the Oracle Date column (which should be second precision) which looks good to me. Also, I'm surprised that 11:20:27 does not equal 11: 20: 27.0. Or is it related to time zones?

Any help is appreciated.

Thorsten

0


source to share


2 answers


Ok, worked some more on it ...

  • Oracle Date columns store values ​​to the nearest second.
  • Java dates contain milliseconds, but they are usually not printed. So,

    expected:

was actually created by date like 11: 20: 27,345, which of course does not equal 11: 20: 27.0



Decision:

  • either use full full dates for storage and retrieval, or
  • get hibernate to generate the correct Oracle datatype (TIMESTAMP) - this is very dependent on the dialect specified in the hibernate config (OracleDialect and Oracle10gDialect create different types).
+1


source


If you are comparing java.util.Date

with a java.sql.Date

that both represent the same point in time, it equals(Object)

will return false (it considers two objects of different classes to be never equal ),



Your tests should take this into account. The easiest way to do this is to convert dates to UNIX times (for example java.util.Date.getTime()

) and compare these values.

0


source







All Articles