a1ex07
a1ex07

Reputation: 37354

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.

Upvotes: 5

Views: 2962

Answers (3)

Aakash
Aakash

Reputation: 11

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

Upvotes: 1

Yada
Yada

Reputation: 31225

How about changing the ZERO dates to null.

Upvotes: 0

Ondra Žižka
Ondra Žižka

Reputation: 46796

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

Upvotes: 7

Related Questions