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