To quote Gavin,
Quote:
Why would you want to pollute your domain model with JDBC-specific classes from the package java.sql????
Your domain model should not have dependencies to persistence infrastructure, if possible.
There are two downsides to
not using a java.sql.Timestamp, in my opinion (note, I talk about Timestamp, here but there are similar considerations for java.sql.Date and java.sql.Time):
- there is a rare possibility of losing the nanosecond portion of the time if you load a date from the database, copy the value in that date to a java.util.Date object (that is, a java.util.Date that
is not a java.sql.Timestamp), and then re-persist it to the database. As far as the database is concerned, there may be a slight difference in times.
- java.sql.Timestamp.equals(java.util.Date) always returns false if the java.util.Date is not a java.sql.Timestamp being treated as a date. It is a very subtle but important thing to understand when considering date equality.
That said, there are few good cases where you should ever be using equality comparisons on dates as a conditional and unless you are doing some sort of realtime system, who cares about a few nanoseconds difference? For most applications, using java.util.Date in the model is completely acceptable.
I feel Gavin's point is much more important in this argument, though: java.sql.Date/Timestamp/Time are JDBC persistence related object types. A more extreme form of the usual argument for java.sql.Timestamp would be:
Well, the database stores strings as an array of bytes, so I should make my firstName property of type byte[], right?
Now, with all of that said, it eventually comes down to a matter of taste. If you choose to use java.sql.Timestamp, fine. But you should probably (as is said in
Hibernate in Action) be consistent, no matter which path you choose and use whichever you choose throughout your model.
I hope I've summarized most of the issues here, but the mentioned threads do a much better job of getting into the details of the matter. I wonder if anyone would be interested in writing an entry in the FAQs about this? I would offer to, but I'm not sure I have a good grasp of all the subtleties.