ERROR: Field 'PostId' doesn't have a default value Exception in thread "main"

Posted yueyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" 相关的知识,希望对你有一定的参考价值。

 

  例子:    

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)
控制台出现的错误

 

 解决问题步骤:

  1. 查看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里在插入语句,造成冲突,所以出错。

 

 

 

 





以上是关于ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" 的主要内容,如果未能解决你的问题,请参考以下文章

ERROR: Field 'culture_title' doesn't have a default value 已解决

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

Mysql Error Code: 1054. Unknown column 'typeName' in 'field list'

Error updating database. Cause: java.sql.BatchUpdateException: Field 'id' doesn't have a

mysql命令 出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'

MySQL Error: 1364 (Field 'reason_id' doesn't have a default value)