ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

  例子:    

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" org.hibernate.exception.GenericJDBCException: could not execute statement

原文地址:https://www.cnblogs.com/yueyou/p/9501898.html

时间: 2024-09-30 21:57:59

ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement的相关文章

Atitit. &#160;Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation problem:

Atitit.  Exception in thread "main" java.lang.Error: Unresolved compilation problem: 1.1. 产看是否产生class文件,以及class体积大小1 1.2. 观察ide源文件,左边有个红叉叉.1 1.3. Resin等服务器锁定了原class文件,导致新生成的class无法替换..1 1.1. 产看是否产生class文件,以及class体积大小 也生成class文件了.但是包这个错..不过大小只有1k

解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)

当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOException { ProcessBuilder builder = new ProcessBuilder(); Process process = builder.command("dir d:\\").start(); InputStream inputStream = process.g

Exception in thread &quot;main&quot; javax.validation.ValidationException: Unable to find a default provider

Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider 在使用Spring MVC+Mybatis整合的时候出现上面的问题 要将下面三个包加入项目的BuildPath中才行:    hibernate-validator-4.2.0.Final.jar    validation-api-1.0.0.GA.jar    slf4j-api-1.6

异常-Exception in thread &quot;main&quot; net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: &lt;EOF&gt; after :

1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after : at net.sf.jsqlparser.parser.CCJSqlParserTokenManager.getNextToken(CCJSqlParserTokenManager.java:3124)

【异常】Maxwell异常 Exception in thread &quot;main&quot; net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: &lt;EOF&gt; after : &quot;&quot;

1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after : "" at net.sf.jsqlparser.parser.CCJSqlParserTokenManager.getNextToken(CCJSqlParserTokenManag

Exception in thread &quot;main&quot; java.lang.Error: 无法解析的编译问题:

public class Demo3 { public static void main(String[] args) {   CycLink cyclink=new CycLink();  CycLink.setLen(10);  CycLink.CreateLink();  CycLink.show();     CycLink.setK(3);  CycLink.setM(3);  CycLink.play();   } }class Child{ int no; Child nextCh

Hibernate中Caused by: java.sql.SQLException: Field &#39;address_id&#39;doesn&#39;t have a default value

在学习Hibernate中,相信很多人都遇到过: Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement Caused by: java.sql.SQLException: Field 'address_id' doesn't have a default value 很是纳闷,明明我的address_id是主键,为毛还提示我不存在默认值?主

ERROR: Field * doesn&#39;t have a default value 已经解决

ERROR: Field 'culture_title' doesn't have a default value 今天做项目,在插入数据时出现了这个从没遇到的异常,结果足足纠缠了十几个小时后终于搞定了,看了98%的异常分析都是针对组件id出现了类似的异常,而针对property出现同类似的异常,分析相对较少, hibernate    property出现 Field 'culture_title' doesn't have a default value   异常分析: 在MySQL 5.0

HTTP Status 500 - JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [HY000]; error code [1364]; could not execute statement; nested exception is org.hibernate.exception.Ge

HTTP Status 500 - JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [HY000]; error code [1364]; could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement 翻译