Field 'id' doesn' t have a default value problem solution
I suddenly wanted to review the reading and writing of the database, so I created a separate table with MySQL (see Code 1) and wrote an application with hibernate to query and modify data normally. At the beginning, the data was manually input in the MySQL GUI tool, so it was annoying to use the program to generate some data in batches, so the trouble also followed ......
Code 1
SQL code
- Create Table 'stput'. 'ticket '(
- 'Id' int (11) not null,
- 'State' tinyint (1) not null,
- 'Grad' int (2) not null,
- Primary Key ('id ')
- ) Engine = InnoDB default charset = GBK
Run the Java code for inserting data and report an exception: Field 'id' doesn' t have a default value. That's right. How can I have a default value for my primary key? I don't understand. Google gave this exception a big result. There are two methods to summarize:
Solution 1: open my. ini, find SQL-mode = "strict_trans_tables, no_auto_create_user, no_engine_substitution", change to SQL-mode = "no_auto_create_user, no_engine_substitution"
Solution 2: MySQL 5 uses a strict mode which needs to be disabled. in Windows, Goto start --> programs --> mysql-> MySQL instance config wizard. follow through the reconfigure instance option --> detailed configuration --> continue next a few screens. at the bottom under enable TCP/IP option there is 'Enable strict mode '. deslect this option (no tick ). save changes and MySQL will restart.
I tried it all, it's useless, and I'm a fedora. I don't have an INI file, but a CNF file (My. CNF is under/etc)
This keyword does not seem to be able to find a solution. Instead, find the setting of the primary key in hibernate. I found a solution! It turned out to be the setting of the. HBM. xml file. Change the attribute of the primary key:
XML Code
- <Generator class = "assigned"/>
Run it again. Because assigned indicates that the primary key is manually allocated, while native indicates that the primary key value is automatically given by the database. I used to add native without thinking about it. I don't know what it means. In addition, if I used to insert data into a table, the primary keys were "auto increment, so there is no problem. This time it was exposed.
To sum up, there is actually a lack of comprehensive understanding of Hibernate and its various settings. Because at the beginning, learning Hibernate is also based on "requirements", rather than learning it by means of learning it. You can get started without fully understanding the various settings.
Field 'id' doesn' t have a default value problem solution