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

来源:互联网 发布:淘宝宝贝如何优化 编辑:程序博客网 时间:2024/06/04 18:19
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生成数据库表的时候多会出现类似问题!!!所以建议自己先建表微笑

建议到*.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 ;


手动设置完该列属性重新运行程序发现通过了!!!!!

0 0
原创粉丝点击