jump to navigation

hibernate mysql date ClassCastException September 27, 2006

Posted by javafoo in hibernate, javanotes, mysql.
1 comment so far

java.lang.ClassCastException
[java]     at org.hibernate.type.DateType.deepCopyNotNull(DateType.java)
[java]     at org.hibernate.type.NullableType.deepCopy(NullableType.java)

Does this look familiar. This happens when you define a java.util.Date field in the POJO and try to map it to a mysql ‘date’ datatype. This happens because a ‘date’ datatype represents only the date part and does not include the time part in mysql world, whereas java.util.Date is date+time. Although in Oracle and DB2 and probably other databases a date datatype is actually date+time. If the corresponding field is defined as a ‘datetime’ type in mysql then you should be fine. Also when using hibernate cfg doclet to create the database schemas do not explicitly say type=”date”, let reflection take care of it. Otherwise doclet will map it to date datatype for mysql, which will cause you this grief, leave it to doclet and it will correctly map it to datetime.