Hibernate: "id field has no default value"

I am faced with what, in my opinion, is a simple problem with Hibernate, but I cannot solve it (Hibernate forums are not available, of course, they do not help).

I have a simple class that I would like to keep, but keep getting:

SEVERE: Field 'id' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [hibtest.model.Mensagem] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) [ a bunch more ] Caused by: java.sql.SQLException: Field 'id' doesn't have a default value [ a bunch more ] 

The corresponding code for the persistent class is:

 package hibtest.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Mensagem { protected Long id; protected Mensagem() { } @Id @GeneratedValue public Long getId() { return id; } public Mensagem setId(Long id) { this.id = id; return this; } } 

And the actual current code is simple:

 SessionFactory factory = new AnnotationConfiguration() .configure() .buildSessionFactory(); { Session session = factory.openSession(); Transaction tx = session.beginTransaction(); Mensagem msg = new Mensagem("YARR!"); session.save(msg); tx.commit(); session.close(); } 

I tried some “strategies” in the GeneratedValue annotation, but it just doesn't work. Initializing id doesn't help either! (e.g. Long id = 20L ).

Can anyone shed some light?

EDIT 2: confirmed: messing with @GeneratedValue(strategy = GenerationType.XXX) does not solve it

SOLVED: re-creating the database solved the problem

+115
java hibernate jpa persistence
Apr 29 '09 at 22:13
source share
23 answers

Sometimes changes made to the model or to the ORM may not accurately reflect on the database even after SchemaUpdate executed.

If the error actually has no reasonable explanation, try to recreate the database (or at least create a new one) and fake it with SchemaExport .

+99
May 11 '09 at 3:52
source share

If you want MySQL to automatically create primary keys, you need to say this when creating the table. You do not have to do this in Oracle.

In the main key, you should enable AUTO_INCREMENT . See the example below.

 CREATE TABLE `supplier` ( `ID` int(11) NOT NULL **AUTO_INCREMENT**, `FIRSTNAME` varchar(60) NOT NULL, `SECONDNAME` varchar(100) NOT NULL, `PROPERTYNUM` varchar(50) DEFAULT NULL, `STREETNAME` varchar(50) DEFAULT NULL, `CITY` varchar(50) DEFAULT NULL, `COUNTY` varchar(50) DEFAULT NULL, `COUNTRY` varchar(50) DEFAULT NULL, `POSTCODE` varchar(50) DEFAULT NULL, `HomePHONENUM` bigint(20) DEFAULT NULL, `WorkPHONENUM` bigint(20) DEFAULT NULL, `MobilePHONENUM` bigint(20) DEFAULT NULL, `EMAIL` varchar(100) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

Here Entity

 package com.keyes.jpa; import java.io.Serializable; import javax.persistence.*; import java.math.BigInteger; /** * The persistent class for the parkingsupplier database table. * */ @Entity @Table(name = "supplier") public class supplier implements Serializable { private static final long serialVersionUID = 1L; @Id **@GeneratedValue(strategy = GenerationType.IDENTITY)** @Column(name = "ID") private long id; @Column(name = "CITY") private String city; @Column(name = "COUNTRY") private String country; @Column(name = "COUNTY") private String county; @Column(name = "EMAIL") private String email; @Column(name = "FIRSTNAME") private String firstname; @Column(name = "HomePHONENUM") private BigInteger homePHONENUM; @Column(name = "MobilePHONENUM") private BigInteger mobilePHONENUM; @Column(name = "POSTCODE") private String postcode; @Column(name = "PROPERTYNUM") private String propertynum; @Column(name = "SECONDNAME") private String secondname; @Column(name = "STREETNAME") private String streetname; @Column(name = "WorkPHONENUM") private BigInteger workPHONENUM; public supplier() { } public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getCountry() { return this.country; } public void setCountry(String country) { this.country = country; } public String getCounty() { return this.county; } public void setCounty(String county) { this.county = county; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getFirstname() { return this.firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public BigInteger getHomePHONENUM() { return this.homePHONENUM; } public void setHomePHONENUM(BigInteger homePHONENUM) { this.homePHONENUM = homePHONENUM; } public BigInteger getMobilePHONENUM() { return this.mobilePHONENUM; } public void setMobilePHONENUM(BigInteger mobilePHONENUM) { this.mobilePHONENUM = mobilePHONENUM; } public String getPostcode() { return this.postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getPropertynum() { return this.propertynum; } public void setPropertynum(String propertynum) { this.propertynum = propertynum; } public String getSecondname() { return this.secondname; } public void setSecondname(String secondname) { this.secondname = secondname; } public String getStreetname() { return this.streetname; } public void setStreetname(String streetname) { this.streetname = streetname; } public BigInteger getWorkPHONENUM() { return this.workPHONENUM; } public void setWorkPHONENUM(BigInteger workPHONENUM) { this.workPHONENUM = workPHONENUM; } } 
+52
May 28 '11 at 11:48
source share

you must use the update in the hbm2ddl property. make changes and update them in Create to create the table.

 <property name="hbm2ddl.auto">create</property> 

It worked for me.

+12
Feb 10 '15 at 20:02
source share

Take a look at the GeneratedValue strategy. This usually looks something like this:

 @GeneratedValue(strategy=GenerationType.IDENTITY) 
+9
Apr 29 '09 at 23:06
source share

Another suggestion is to verify that you are using a valid type for the automatically generated field. Remember that it does not work with string, but works with Long:

 @Id @GeneratedValue(strategy=GenerationType.AUTO) public Long id; @Constraints.Required public String contents; 

The above syntax worked to generate tables in MySQL using Hibernate as a JPA 2.0 provider.

+3
Jul 19 2018-12-17T00:
source share

I had the same problem. I found the tutorial Example of matching single-user hibernation using the Annotation foreign key and followed it step by step as shown below:

Create a database table using this script:

 create table ADDRESS ( id INT(11) NOT NULL AUTO_INCREMENT, street VARCHAR(250) NOT NULL, city VARCHAR(100) NOT NULL, country VARCHAR(100) NOT NULL, PRIMARY KEY (id) ); create table STUDENT ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, entering_date DATE NOT NULL, nationality TEXT NOT NULL, code VARCHAR(30) NOT NULL, address_id INT(11) NOT NULL, PRIMARY KEY (id), CONSTRAINT student_address FOREIGN KEY (address_id) REFERENCES ADDRESS (id) ); 

Here are the entities with the above tables

 @Entity @Table(name = "STUDENT") public class Student implements Serializable { private static final long serialVersionUID = 6832006422622219737L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; } @Entity @Table(name = "ADDRESS") public class Address { @Id @GeneratedValue @Column(name = "ID") private long id; } 

The problem is resolved.

Note. The primary key must be set to AUTO_INCREMENT

+3
Oct. 15 '16 at 5:31 on
source share

I came here because of an error message, it turned out that I have two tables with the same name.

+2
Nov 09 '15 at 19:06
source share

Just add a non-null constraint

I had the same problem. I just added a null-null constraint in the xml mapping. Worked

 <set name="phone" cascade="all" lazy="false" > <key column="id" not-null="true" /> <one-to-many class="com.practice.phone"/> </set> 
+2
Feb 22 '16 at 10:59
source share

Removing the table from the database manually and then re-executing the application worked for me. I think in my case the table was not created properly (with restrictions).

+2
Jul 01 '18 at 21:34
source share

I had this problem. My mistake was that I set the inserted and updated files to false and tried to set the field in the request. This field is set to NON NULL in the database.

 @ManyToOne @JoinColumn(name="roles_id", referencedColumnName = "id", insertable = false, updatable = false, nullable=false) @JsonBackReference private Role role; 

Later I changed it to - insertable = true, updated = true

 @ManyToOne @JoinColumn(name="roles_id", referencedColumnName = "id", insertable = true, updatable = true, nullable=false) @JsonBackReference //@JsonIgnore private Role role; 

This worked perfectly later.

+2
Apr 25 '19 at 16:35
source share

In addition to what was mentioned above, do not forget when creating the sql table to do AUTO INCREMENT, as in this example

 CREATE TABLE MY_SQL_TABLE ( USER_ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, FNAME VARCHAR(50) NOT NULL, LNAME VARCHAR(20) NOT NULL, EMAIL VARCHAR(50) NOT NULL ); 
+1
Jul 22 '18 at 6:48
source share

Maybe this is a problem with the table schema. drop the table and run the application again.

+1
Dec 14 '18 at 15:15
source share

Please check if the default value for the column id in table.if table does not make it the default

0
Dec 21 '10 at 9:54
source share

I had the same problem. I used the connection table and everything that I had with the id field of the row and two foreign keys. I do not know exactly what was called, but I did the following

  • Updating MySQL to the community 5.5.13
  • Rename the class and table
  • Make sure I have hashcode and equals methods

     @Entity @Table(name = "USERGROUP") public class UserGroupBean implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "USERGROUP_ID") private Long usergroup_id; @Column(name = "USER_ID") private Long user_id; @Column(name = "GROUP_ID") private Long group_id; 
0
Jun 23 '11 at 16:30
source share

The same exception was thrown if the database table had an old unmanaged column.

For example: attribute_id NOT NULL BIGINT(20), and attributeId NOT NULL BIGINT(20),

After removing the unused attribute in my case contractId the problem was resolved.

0
Feb 27 '17 at 11:30
source share

I had this problem, by mistake I put the @Transient annotation over this particular attribute. In my case, this error makes sense.

0
Jun 24 '17 at 13:27
source share

This happened to me with @ManyToMany attitude. I annotated one of the fields with @JoinTable , I removed it and used the mappedBy attribute in @ManyToMany instead.

0
Jul 20 '17 at 2:53 on
source share

I tried the code and in my case the code below solves the problem. I did not install the circuit correctly

 @Entity @Table(name="table" ,catalog="databasename" ) 

Try adding ,catalog="databasename" same as me.

 ,catalog="databasename" 
0
Jul 29 '18 at 10:00
source share

In my case, I changed these violating tables and the "id" field in question, I did this AUTO_INCREMENT, I still need to find out why during the deployment he did not do it "AUTO_INCREMENT", so I have to do it myself!

0
Dec 05 '18 at 9:46
source share

How about this:

 <set name="fieldName" cascade="all"> <key column="id" not-null="true" /> <one-to-many class="com.yourClass"/> </set> 

Hope this helps you.

0
Dec 05 '18 at 9:56
source share

Try changing the type of the Long object to the Long type of the primitive (if you are using primitives).

I had the same problem, and changing the type helped me.

0
Jan 17 '19 at 7:46
source share

“The 'id' field does not have a default value" because you did not declare GenerationType.IDENTITY in GeneratedValue Annotation.

 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; 
0
Jul 12 '19 at 19:54
source share

Add the hashCode() method to your Bean Class object and try again

-one
May 16 '10 at 19:36
source share



All Articles