Primary key default value error: java.sql.SQLException: Field'id' doesn't have a default value

Problem phenomenon:

Today, when calling the Controller interface to insert data into the Mysql database, the following error occurred:

java.sql.SQLException: Field 'id' doesn't have a default value


problem analysis:

By looking at the error message, we can see: it is because the field oid has no default value. Very strange error message, because I clearly assigned a value to oid in the Controller layer:

By checking the information on the Internet, I found that the solutions almost all said:

Just let you set the database primary key to auto-increment !

Although oid is indeed the primary key, there is nothing wrong with it, but why does the primary key have to be auto-incremented to succeed? Do the primary keys of all entity classes have to be auto-incremented? What if I want to assign it myself?

It is a pity that these articles did not explain this deeper reason! This involves the truth of " Teaching him to fish is better than teaching him to fish "!

Rather than telling me how to do it, what I want to know is why I do it!

By analyzing the problem of primary key self-increment, we can first focus on the database table:

From the table properties, it can be seen that my primary key does not have auto-increment, and if I set auto-increment, I can indeed start the project normally!
But there is a big problem:
That is, if I manually assign a value to oid, it will be invalid, and the database will still assign a value to oid according to the rules of self-increment:

That is to say, manual assignment and auto-increment can only choose one, and when auto-increment is set, manual assignment will be invalid!
This is not the result I want, so can I manually assign the primary key oid successfully?
Since the database can't solve this problem, we can only check the model entity class, which can be found by checking the configuration:
I used the @GeneratedValue annotation in the entity class, and then looked at the value of strategy as GenerationType.IDENTITY, only to find the problem!!!

Because:  The characteristics of the GenerationType.IDENTITY primary key generation strategy are:

The primary key is generated by the database , and the database self-growth method must be used (otherwise the dao layer will report an error when inserting data), but Oracle does not support this method.

Therefore, if you want to realize my idea, you must modify  the attribute value of strategy  . Through learning, I learned:

The strategy attribute provides four values:
    GenerationType.AUTO : The primary key is controlled by the program. It is the default option. If it is not set, this
    GenerationType . IDENTITY : The primary key is generated by the database and must be self-growth by the database (otherwise the dao layer inserting data will report an error), Oracle This method is not supported
    GenerationType . SEQUENCE : Generate primary keys through database sequences, MYSQL does not support
    GenerationType . Table : Provide specific databases to generate primary keys, this method is more conducive to database transplantation

So we can remove the  @GeneratedValue annotation, I think this is the fastest way.

Then someone will ask: Why should I add  @GeneratedValue annotation in the first place?

This involves the role of @GeneratedValue annotation, here I briefly mention:

The @GeneratedValue annotation can be used to provide a primary key generation strategy.For example, the primary key of the database is incremented by 1 each time, but it can be modified through this annotation, and there are some other functions, just wait for the friends to discover by themselves.


Solution:

Remove  @GeneratedValue(strategy = GenerationType.IDENTITY)  :

 
 

success:

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/109203750