There seems to be an historic problem with hibernate's date/time handling (see
http://forum.hibernate.org/viewtopic.ph ... highlight=) but since this post does not suppose any possible solution, I'm looking for an solution to make following code work.
Example:
Code:
project=(Project)hibernateSession.get(Project.class, projectId);
project.setDate(new Date());
hibernateSession.save(project);
hibernateSession.evict(project);
// reread the same object instance
Project currentProject=(Project)
hibernateSession.get(Project.class, project.getProjectId());
The code creates an object, saves it with a modified date entry, and reloads another instance of it. Afterwards equals method will fail
Code:
currentProject.equals(project)==FALSE !!!
Analysis:The problem is caused by the equals method of the date objects:
Code:
project.getDate().equals(currentProject.getDate())==TRUE
currentProject.getDate().equals(project.getDate())==FALSE
The mapping for date is type="timestamp", after rereading the object the class of the date object changed from java.util.Date to java.sql.Timestamp. Bad luck: the hashcode of both classes is calculated in the same way, so
Code:
(currentProject.hashCode()==project.hashCode())==TRUE
Consequence:
It matters where the objects come from, hibernate is not "transparent" when dealing with hibernated objects.
Question:
Has someone else had problems with hibernate's date handling? Which could be a good workaround, that has no other "side effects"?
Implement a HibernateUtil.dateEquals() method and modify all the object.equals-methods? (Dirty since object.equals will work, but other programmers using my api may experience the same strange problems as I)
Write special code into getter/setter do to the mapping? (Dirty since I thought that this is exactly what hibernate should be for)
Any suggestions welcome!