Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Date/Datetime columns and Java persistence

I have a Mysql table with DATE column , default '0000-00-00'. If I try to call, for instance, em.find(MyTable.class,pk_value); and it happens to be a record in database with default date value '0000-00-00', an exception is thrown( "java.sql.SQLException: Value ... cannot be represented as java.sql.Date". The same error happens for DateTime columns with default '0000-00-00 00:00:00'.
Is it any way to tell EntityManager that such values are ok ? Thanks.

like image 201
a1ex07 Avatar asked Jan 15 '10 21:01

a1ex07


2 Answers

What helps is setting JDBC driver's zeroDateTimeBehavior property to convertToNull.

See http://ondra.zizka.cz/stranky/programovani/java/index.texy (look for "SQLException for zero DATETIME or TIMESTAMP column? Use zeroDateTimeBehavior").

jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull

like image 64
Ondra Žižka Avatar answered Oct 15 '22 16:10

Ondra Žižka


By setting the zeroDateTimeBehavior=convertToNull property we can avoid this problem. For details please check this link.

like image 1
Aakash Avatar answered Oct 15 '22 16:10

Aakash