21

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'); 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; my mysql jar is mysql-connector-java-5.0.8

2
  • It means your supplier id doesn't have a default value, and you have to insert one. Commented Oct 13, 2014 at 9:18
  • The solution for a similar problem could be found in: stackoverflow.com/questions/804514/… Commented Feb 27, 2017 at 11:38

7 Answers 7

34

The error is self explanatory. Your column supplier_id does not have a default value. So during insertion, mysql cannot figure out what to insert in the column supplier_id. You can do either of the three things :-
1. Add a default value to the column supplier_id Using -

ALTER TABLE `xxx` ALTER `supplier_id` SET DEFAULT NULL


2. Supply some value to the supplier_id column during insertion.
3. Add an auto increment to the column and add a primary key to it using the code :-

ALTER TABLE `xxx` CHANGE `supplier_id` `supplier_id` INT(10)AUTO_INCREMENT PRIMARY KEY;
Sign up to request clarification or add additional context in comments.

3 Comments

my fields type is VARCHAR and it is not PRIMARY KEY , is there not anthor way to solve this error?
OK, so 3rd option is not applicable. So, go with the 1st option (recommended). Add a default value to the column
pass the sql query that I mentioned in the 1st option to make the default value as null for the supplier_id column
4

To solve this, either supply a value for supplier_id when you do the INSERT, or make the column nullable in the DB.

Comments

1

There might be two cases.

  1. You have declared the column's default value as NONE and are not not passing any value to it while inserting.

  2. You have declared the column in database, but you have not declared the same in your entity object, which is causing the error.

Comments

1

I added this property

<property name="hbm2ddl.auto">create</property>

into hibernate.cfg.xml file, and it worked for me.

1 Comment

This is very dangerous, it will drop all the existing tables and create new ones as blank tables to match our requirements.
1

This Error say Field has a Default value means it not to be auto incremental So first thing you do is,

go for Mysql and select Alter Table and check the Auto Increment

Even So, you Getting this Error Follow below instruction.

  • Sometimes changes made to the model or to the ORM may not reflect accurately on the database even after execution of SchemaUpdate. the error actually seems to lack a sensible explanation, try recreating the database or at least creating a new one and scaffolding it with SchemaExport

Comments

0

You are using table where you have supplier_id column . 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.

Comments

-1

In my case, the problem was that the primary key column was not set as AUTO_INCREMENT

Here's how I solved it:

alter table library modify column Id int NOT NULL AUTO_INCREMENT;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.