Hibernate: "Field 'id' doesn't have a default value" Hibernate: "Field 'id' doesn't have a default value" java java

Hibernate: "Field 'id' doesn't have a default value"


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

If the error actually seems to lack a sensible explanation, try recreating the database (or at least creating a new one) and scaffolding it with SchemaExport.


If you want MySQL to automatically produce primary keys then you have to tell it when creating the table. You don't have to do this in Oracle.

On the Primary Key you have to include 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's the 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;  }}


Take a look at GeneratedValue's strategy. It typically looks something like:

@GeneratedValue(strategy=GenerationType.IDENTITY)