ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: coul
程序员文章站
2022-06-27 14:08:26
例子: 在执行数据保持时提示session.save(p); 的错误。 INFO: HHH000232: Schema update complete Hibernate: insert into Post (postName) values (?) 八月 19, 2018 5:07:17 下午 o ......
例子:
post p = new post(); p.setpostid(3); p.setpostname("技术");
在执行数据保持时提示session.save(p); 的错误。
info: hhh000232: schema update complete hibernate: insert into post (postname) values (?) 八月 19, 2018 5:07:17 下午 org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions warn: sql error: 1364, sqlstate: hy000 八月 19, 2018 5:07:17 下午 org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions error: field 'postid' doesn't have a default value exception in thread "main" org.hibernate.exception.genericjdbcexception: could not execute statement at org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconverter.java:54) at org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:126) at org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:112) at org.hibernate.engine.jdbc.internal.resultsetreturnimpl.executeupdate(resultsetreturnimpl.java:190) at org.hibernate.id.identitygenerator$getgeneratedkeysdelegate.executeandextract(identitygenerator.java:96) at org.hibernate.id.insert.abstractreturningdelegate.performinsert(abstractreturningdelegate.java:58) at org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:3032) at org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:3558) at org.hibernate.action.internal.entityidentityinsertaction.execute(entityidentityinsertaction.java:97) at org.hibernate.engine.spi.actionqueue.execute(actionqueue.java:488) at org.hibernate.engine.spi.actionqueue.addresolvedentityinsertaction(actionqueue.java:193) at org.hibernate.engine.spi.actionqueue.addinsertaction(actionqueue.java:177) at org.hibernate.engine.spi.actionqueue.addaction(actionqueue.java:212) at org.hibernate.event.internal.abstractsaveeventlistener.addinsertaction(abstractsaveeventlistener.java:324) at org.hibernate.event.internal.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:288) at org.hibernate.event.internal.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:194) at org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:125) at org.hibernate.event.internal.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:209) at org.hibernate.event.internal.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:55) at org.hibernate.event.internal.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:194) at org.hibernate.event.internal.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:49) at org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90)
解决问题步骤:
- 查看post.hbm.xml 文件
<class name="hibernate.model.post" table="post"> <id name="postid"> <!-- 主键生成策略 --> <generator class="native"></generator> </id> <property name="postname"/>
2. <!-- 主键生成策略 -->
<generator class="native"></generator>
3. 删掉。
4. 重新运行。
原因:
post.hbm,xml配置文件里的<generator class="native"></generator>对主键已经默认赋值了,在post.java里在插入语句,造成冲突,所以出错。