The problem with java.sql.Timestamp is really annoying - especially for testing. The simplest test doesn't work as expected.
Code:
MyEntity entity = new MyEntity();
entity.setCreated(new Date(10000001));
myEntityDao.persist(entity);
List<MyEntity> entities = myEntityDao.loadAll();
assertTrue(CollectionUtils.contains(entities.iterate(), entity)); // <- FAILED!
Entity.equal() method for entity is implemented as (such a comparison is used for tests only)
Code:
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
I use Hibernate as JPA provider with XML configuration and I found now way how to make Hibernate return java.util.Date instead of java.sql.Timestamp.
The only thing that helps me - is to implement entity setter as a "deep" copier
Code:
public void setCreated(Date created) {
if (created != null) {
this.created = new Date(created.getTime());
} else {
this.created = null;
}
}
But that's not good.