Original article
Hibernate, java.util.Date and java.sql.Date Issues
One issue that can crop up during unit testing is problems with date mapping. In SQL terms dates to a “day” level of precision can be specified using a DATE column and dates with a “time” element can be mapped using a TIMESTAMP (or MySQL DATETIME) column. These map respectively to the Java SQL types java.sql.Date and java.sql.Timestamp. Note that a java.util.Date object can specify times, up to a millisecond level of precision, whereas a java.sql.Timestamp object can handle nanosecond-level precision.
If we are using Hibernate to map a persistent class, and one of the fields is a day-level-precision Date field, we could map it like so:
In a unit test to exercise the persistence mapping, it could go something like the following:
Foo f = new Foo();
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
f.setStartDate(now);
and then test the retrieved persistent entity like so:
fooEntity = session.load(Foo.class, id);
assertEquals(f.getStartDate(), fooEntity.getStartDate());
The assertion will fail. The reason for this is that the call to fooEntity.getStartDate() returns an instance of java.sql.Date, whereas we are comparing against a java.util.Date. Since java.sql.Date is a subclass of java.util.Date, our tests will run without any ClassCastExceptions, but any equality checks will fail. The reason for this is apparent from the java.sql.Date javadoc:
To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
This means that any Date objects we retrieve from our Hibernate-mapped objects will have an effective time of midnight , i.e.something like 25-Jan-2006 00:00:00. We cannot just use the getTime() method to retrieve a long equaivalent of the date value either, as the they will ave the same problem.
What we can do is “normalize” the Date object before we persist it, like so:
private Date normalize(Calendar cal) {
cal.set(Calendar.MILLISECOND, 0);
cal.set(2006,0,25,0,0,0);
return cal.getTime();
}
...
Date now = normalize(cal);
event.setStartDate(now);
Now our asserts will pass without a problem.
There’s an entertaining overview of the bigger picture here.
Freitag, 15. Oktober 2010
Dienstag, 12. Oktober 2010
Java exceptions
Unchecked exceptions :
* represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."
* are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException
* a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)
Checked exceptions :
* represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
* are subclasses of Exception
* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)
It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).
* represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."
* are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException
* a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)
Checked exceptions :
* represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
* are subclasses of Exception
* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)
It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).
Montag, 26. April 2010
Freitag, 16. April 2010
Vulkanausbruch in Island
Dienstag, 16. März 2010
Witziges Code Schnipsel
Der Fund ist mir in einem Forum gelungen und ich finde das Schnipsel rein optisch schon sehr ansprechend :)
static int inverse(int _) {
int __ = _;
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
return _;
}
static int inverse(int _) {
int __ = _;
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
_ *= -~-~-(__ * _);
return _;
}
Samstag, 6. Februar 2010
Guten Morgen
So es ist doch recht früh aber ich kann nimmer schlafen und da ich schon lang nix mehr geschrieben hab denk ich mir nütze ich die Zeit und schreib mal wieder was :) .
Es sind sicher eine der schönsten Wochen in meinem Leben Katharina ist wieder Gesund, Johanna ist wieder nach Hause gekommen und auch Gesund und fühlt sich wohl bei uns und ich hab meine Prüfung geschafft (SCJP) und das Sahnehäubchen es ist wieder über Nacht ein bissi Schnee vom Himmel gefallen. :)
Es sind sicher eine der schönsten Wochen in meinem Leben Katharina ist wieder Gesund, Johanna ist wieder nach Hause gekommen und auch Gesund und fühlt sich wohl bei uns und ich hab meine Prüfung geschafft (SCJP) und das Sahnehäubchen es ist wieder über Nacht ein bissi Schnee vom Himmel gefallen. :)
Donnerstag, 28. Januar 2010
Was ist eine Kuh
Eine Kuh ist ein Tier, das hat Kleiderhaken am Kopf, das hat'n roten Waschlappen im Maul, das hat schwarze Rasierpinsel am Kopf, das hat'n großen alten Handschuh zwischen den Beinen, das hat'n Strick am Hintern und einen schwarz weiß gefleckten Bettvorleger um den Bauch gewickelt.
:) Das ist eine Kuh :)
:) Das ist eine Kuh :)
Abonnieren
Posts (Atom)