大家好,又见面了,我是你们的朋友全栈君。
问题描述:
最近在用Hibernate操作mysql的过程中(往mysql里添加数据,因为在代码中设置的主键即id是自增长的,所以插入数据的时候默认没有给id赋值),所以就遇到了这样的一个问题:ERROR: Field ‘id’ doesn’t have a default value
Exception in thread “main” org.hibernate.exception.GenericJDBCException: Field ‘id’ doesn’t have a default value。
详细信息如下:
五月 14, 2017 8:38:38 上午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Field 'id' doesn't have a default value
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Field 'id' doesn't have a default value
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy8.executeUpdate(Unknown Source)
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:2936)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:727)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:719)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:715)
at hotel.test.Test.main(Test.java:32)
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3361)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3295)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1852)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1975)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2476)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1583)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1859)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1777)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1762)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 23 more
问题原因:
仔细分析一下提示的错误信息,初步判定是因为主键id有关系,经过在网上验证,出现这个问题的原因是在实体类的配置文件中设置主键的生成策略是native,而在数据库中并没有设置主键自增长,因为不匹配所以就会出现这个问题。
解决办法:
1.将数据库中对应表的主键设置为自动增长。
2.修改配置文件主键的生成策略。
总结:
结合最近一段时间研究Hibernate多租户的经历,没有什么困难是我们解决不了的,当我们遇到困难的时候,要冷静的去分析打印出来的日志,结合网上的资料,一定会找到解决办法的。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/105689.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...