[英]java.sql.SQLException: Field 'supplier_id' doesn't have a default value
I got an error message from this:我收到一条错误信息:
java.sql.SQLException: Field 'supplier_id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
Everyone can help me ?大家可以帮帮我吗? my database fields are not empty .
我的数据库字段不为空。 but i want to get this results:
但我想得到这个结果:
insert into xxx(name,password)values('xxx','xxx');
and insert into xxx(name,password,man)values('xxx','xxx','xxx');
并
insert into xxx(name,password,man)values('xxx','xxx','xxx');
both success (both of that in client is success ,but in java code is error,error code at top title), instead of insert into xxx(name,password)values('xxx','xxx')
is false;都成功(在客户端都是成功,但在java代码中是错误,顶部标题的错误代码),而不是
insert into xxx(name,password)values('xxx','xxx')
是假的; my mysql jar is mysql-connector-java-5.0.8我的 mysql jar 是 mysql-connector-java-5.0.8
The error is self explanatory.该错误是不言自明的。 Your column
supplier_id
does not have a default value.您的列
supplier_id
ID 没有默认值。 So during insertion, mysql cannot figure out what to insert in the column supplier_id
.所以在插入过程中,mysql 无法弄清楚要在列
supplier_id
插入什么。 You can do either of the three things :-您可以做以下三件事中的任何一件:-
1. Add a default value to the column supplier_id
Using - 1. 使用-为列
supplier_id
添加默认值
ALTER TABLE `xxx` ALTER `supplier_id` SET DEFAULT NULL
2. Supply some value to the supplier_id
column during insertion. 2. 在插入期间向
supplier_id
ID 列提供一些值。
3. Add an auto increment to the column and add a primary key to it using the code :- 3. 向列添加自动增量并使用以下代码为其添加主键:-
ALTER TABLE `xxx` CHANGE `supplier_id` `supplier_id` INT(10)AUTO_INCREMENT PRIMARY KEY;
要解决此问题,请在INSERT
时为supplier_id
ID 提供一个值,或者使该列在数据库中可以为nullable
。
I added this property我添加了这个属性
<property name="hbm2ddl.auto">create</property>
into hibernate.cfg.xml
file, and it worked for me.进入
hibernate.cfg.xml
文件,它对我有用。
There might be two cases.可能有两种情况。
You have declared the column's default value as NONE and are not not passing any value to it while inserting.您已将该列的默认值声明为 NONE,并且在插入时未向其传递任何值。
You have declared the column in database, but you have not declared the same in your entity object, which is causing the error.您已经在数据库中声明了该列,但是您没有在实体对象中声明相同的列,这会导致错误。
You are using table where you have supplier_id column .您正在使用具有 supply_id 列的表。 it does not have default value ,at insertion time this column not getting any value .
它没有默认值,在插入时此列没有任何值。 initialize supplier_id column with some default value , or use auto increment if you are using supplier_id as a primary key.
使用一些默认值初始化 supply_id 列,或者如果您使用 supply_id 作为主键,则使用自动增量。
In my case, the problem was that the primary key column was not set as AUTO_INCREMENT就我而言,问题是主键列未设置为 AUTO_INCREMENT
Here's how I solved it:这是我解决它的方法:
alter table library modify column Id int NOT NULL AUTO_INCREMENT;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.