gavin wrote:
Dude, Timestamp inherits Date! Think about it. Hibernate has no problem assigning an instance of Timestamp to a property of type Date.
Although Timestamp inherits Date's implementation, it does NOT inherit its semantics. To quote from Sun's javadocs for Timestamp [1.5]:
Quote:
Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.
So although it is legal Java to assign Timestamps to java.util.Date fields, it breaks the semantics and loses information. I just spent many hours dealing with a bug that was caused by this mismatch. It appears that many other users have met a similar fate.
As nearly as I can reckon, the following trivial Type precisely matches the semantics of the java.util.Date class:
Code:
public class TimeMillisType extends org.hibernate.type.TimestampType {
public Date get(ResultSet rs, String name) throws SQLException {
Timestamp timestamp = rs.getTimestamp(name);
if (timestamp == null) return null;
return
new Date(timestamp.getTime()+timestamp.getNanos()/1000000);
}
}
I don't understand why this isn't the default mapping for java.util.Date. It seems like a very large number of people have run afowl of this problem.