Hibernate in caused By:java.sql.SQLException:Field ' address_id ' doesn ' t has a default value

Source: Internet
Author: User

In learning hibernate, I believe many people have met:

Exception in thread ' main ' org.hibernate.exception.GenericJDBCException:could not execute statement
caused By:java.sql.SQLException:Field ' address_id ' doesn ' t have a default value

is very puzzled, obviously my address_id is the primary key, for Mao also hint I do not exist default value? Should the primary key not automatically generate values?

Solve is on the one hand, know why the error is the most important.

Cause of Error:

Here are some of the mistakes I've taken (address_id)

Import Java.util.hashset;import java.util.set;import Javax.persistence.*;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import javax.persistence.Table;@ Entity@table (name= "Personset_inf") public class Personset {@Id @column (name= "person_id")/* @GeneratedValue (strategy= generationtype.identity) */private Integer id;private string name;private string age;


at the beginning, although I specified the primary key and the primary key name, but did not set the address_id as the primary key @GeneratedValue (strategy=generationtype.identity) Self-increment type (the above code is annotated), the running program console will error:

Exception in thread ' main ' org.hibernate.id.IdentifierGenerationException:ids for this class must is manually assigned be Fore calling Save (): Com.mao.PersonSet

Although the error is reported, hibernate has created a table (Personset_inf), note that the table's primary key is not self-growing mode,

And the data you want to insert is not plugged in, you find the error, and then you add the self-growth type to the primary key in the program and rerun the project, you will get a bug:

Exception in thread ' main ' org.hibernate.exception.GenericJDBCException:could not execute statementcaused by: Java.sql.SQLException:Field ' address_id ' doesn ' t have a default value
The reason is: Although you modify the primary key type in the code, but your database table has been built, its primary key is still not self-growth type, the ID is not automatically assigned, and then error: ID has no default value.

Solution:

First confirm that the program has identified the primary key mode, and then delete the database table, rerun the program, create a data table with the primary key mode, so that you can, and the data is successfully inserted into it.


Hibernate in caused By:java.sql.SQLException:Field ' address_id ' doesn ' t has a default value

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.