Our applications make extensive use of date fields that may or may not be set, meaning that whatever the date is for might not yet have happened. That a particular date field is unset, or void, can have important business significance. For this reason we have preferred using Calendar to represent dates as, amongst other things, we can use its clear() method to make it unset. Then in our own DAO's we can test the Calendar date with isTimeSet() and either write a null or a TimeStamp to the database accordingly.
However my intial experiments using Hibernate 2 suggest that when you save a Calendar object that is in an unset state, a zero is written to the database presumably becasue the Calendar.clear() method actually sets the time to zero. As a result when the Calendar object is subsequently reloaded it is set to midnight 1st January 1970 and this will not do at all.
As a relative newbie to Hibernate, I am hoping that there are more experienced members of the community who have found solutions to this problem and can advise on:
1. If I am missing a mapping technique that enables unset Calendar obejcts to written as nulls rather than zero's and visa-versa on reload
2. If not, then has anyone found an elegant way to represent dates such that an unset date is represented as a null in the DB, but as an unset Calendar in the object model?
Thank you all for any comments on this.
|