使用hibernate開發(fā)程序的時候,有的時間字段沒有必要填寫,但是,以后hibernate查詢的時候會報出
“java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp”
的錯誤, 這是因為hibernate認(rèn)為這個不是一個有效的時間字串。
而有效的日期格式為“ 0001-01-01 00:00:00.0 ”
查看了mysql5的幫助文檔對于datetime的解釋如下
Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented 關(guān)于所有Datetime類型由0組成的數(shù)據(jù),這些值不能在java中被可靠的表示
reliably in Java.
Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.
當(dāng)這些值正在從ResultSet容器中讀取時候,Connector/J 3.0.x 一直把他們轉(zhuǎn)換為NULL值。
Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards.
依照J(rèn)DBC和SQL的標(biāo)準(zhǔn)這些值碰到的最正確的處理方式就是在缺省情況下產(chǎn)生異常
This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:
JDBC允許用下列的值對zeroDateTimeBehavior 屬性來設(shè)置這些處理方式,
exception (the default), which throws an SQLException with an SQLState of S1009.
設(shè)置為exception 異常(缺?。┯靡粋€SQLState的s1009錯誤號來拋出一個異常
convertToNull, which returns NULL instead of the date.
設(shè)置為convertToNull,用NULL值來代替這個日期類型
round, which rounds the date to the nearest closest value which is 0001-01-01.
設(shè)置為round,則圍繞這個日期最接近的值(0001-01-01)來代替
修改你的jdbc連接
jdbc:mysql://localhost/schoolmis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull