天天看點

Field 'ID' doesn't have a default value,ecould not execute statement

org.hibernate.exception.GenericJDBCException: could not execute statement

at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)

Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

)

at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)

... 47 more

org.hibernate.AssertionFailure: null id in com.atguigu.hibernate.subclass.Person entry (don't flush the Session after an exception occurs)

原因分析:ID這個屬性沒有預設值,也就是說hibernate辨別符沒有映射成功,資料庫沒能自動設定成功ID這個列屬性!!也就是找資料庫表的設計問題。後來遇到了Field 'TYPE' doesn't have a default value,同樣是在資料庫找到對應表,把TYPE列的屬性不為NULL的限制去問題就解決了,綜上個人覺得,在使用hibernate生成資料庫表的時候多會出現類似問題!!!是以建議自己先建表

Field 'ID' doesn't have a default value,ecould not execute statement

建議到*.hbm.xml檔案檢視對應類的該屬性

我的是

<hibernate-mapping package="com.atguigu.hibernate.entities">

    <class name="Worker" table="WORKER">

        <id name="id" type="java.lang.Integer">

            <column name="ID" />

            <generator class="native" />

        </id>

  </class>

</hibernate-mapping>

即我的辨別符生成器為native辨別符生成器!!!當然,類似 could not execute statement的異常可以考慮更換不同的辨別符生成器或許可以結局問題。

native 辨別符生成器依據底層資料庫對自動生成辨別符的支援能力, 來選擇使用 identity, sequence 或 hilo 辨別符生成器,由于 native 能根據底層資料庫系統的類型, 自動選擇合适的辨別符生成器, 是以很适合于跨資料庫平台開發。OID 必須為 long, int 或 short 類型, 如果把 OID 定義為 byte 類型, 在運作時會抛出異!!!

但是對應資料庫中的列需要手動設定該列的屬性為自動增長,我用的是mysql資料庫。設定該列的屬性用到如下語句!!!

ALTER TABLE persons MODIFY COLUMN `ID`  int(11) NOT NULL AUTO_INCREMENT FIRST ;

手動設定完該列屬性重新運作程式發現通過了!!!!!