views:

1036

answers:

2

Hi,

I'm storing a simple java.util.date in an Oracle XE database via hibernate.

When testing with JUnit if I can retrieve 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 an Oracle Date column (which should have a second-precision) which looks okay to me. Also, I'm surprised that 11:20:27 is not equal to 11:20:27.0. Or does this have to do with timezones?

Any help is welcome.

Thorsten

A: 

If you compare a java.util.Date to a java.sql.Date that both represent the same instant in time, equals(Object) will return false (it considers two objects of different classes to never be equal).

Your tests need to account for that. The easiest way to do this is to convert the dates to UNIX time (e.g. java.util.Date.getTime()) and compare those values.

davetron5000
Dave, thanks for the idea, but that was not causing the problem.
IronGoofy
+1  A: 

Okay, worked some more on it ...

  1. Oracle Date columns only store values with an accuracy of a second.
  2. Java Dates do contain milliseconds, but they are typically not printed. So

    expected:

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

Solution:

  • either only use full second dates to store and retrieve or
  • get hibernate to create the correct Oracle Datatype (TIMESTAMP) - this is very dependent on the dialect specified in the hibernate config (OracleDialect and Oracle10gDialect create different types).
IronGoofy