Field 'id' doesn' t have a default value problem solution

Source: Internet
Author: User
 
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
  1. Create Table 'stput'. 'ticket '(
  2. 'Id' int (11) not null,
  3. 'State' tinyint (1) not null,
  4. 'Grad' int (2) not null,
  5. Primary Key ('id ')
  6. ) 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
  1. <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.