0

I have a Spring Boot User class which always comes up with the error "java.sql.SQLException: Field 'id' doesn't have a default value". I have tried many times to provide a default value, both in the Java class and in the database table, but to no avail. And I have also switched from generation type = auto and = identity, but to no avail. Thank you very much for your help. Here is my Java Class and my Database Table:

package com.ykirby.myfbapp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.beans.factory.annotation.Value;

import java.sql.Timestamp;
import java.util.Date;  
@Entity // This tells Hibernate to make a table out of this class
public class User {
  
    
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @Value("#{User.id ?: 0}")
    
    private int id = 12345;
    
    @Column(name = "fbuserid")
    private String fbuserid;
    @Column(name = "apttime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date apttime;
    @Column(name = "apttitle")
    private String apttitle;
    @Column(name = "aptaddress")
    private String aptaddress;
    @Column(name = "aptlonglat")
    private String aptlonglat;
    @Column(name = "aptdetails")
    private String aptdetails;
public String getFbuserid() {
    return fbuserid;
}
public void setFbuserid(String fbuserid) {
    this.fbuserid = fbuserid;
}
public Date getApttime() {
    return apttime;
}
public void setApttime(Date apttime) {
    this.apttime = apttime;
}
public String getApttitle() {
    return apttitle;
}
public void setApttitle(String apttitle) {
    this.apttitle = apttitle;
}
public String getAptaddress() {
    return aptaddress;
}
public void setAptaddress(String aptaddress) {
    this.aptaddress = aptaddress;
}
public String getAptlonglat() {
    return aptlonglat;
}
public void setAptlonglat(String aptlonglat) {
    this.aptlonglat = aptlonglat;
}
public String getAptdetails() {
    return aptdetails;
}
public void setAptdetails(String aptdetails) {
    this.aptdetails = aptdetails;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}


}

My database schema

Yoni Kirby
  • 23
  • 1
  • 7

3 Answers3

0

You should drop the existing database and re-generate it because sometimes changes done through the model don't reflect properly in the database. While re-generating the database you can scaffolding it with SchemaExport.

0

Does

    @Column(name = "apttitle")
    private String apttitle="default";

work?

袁文涛
  • 735
  • 1
  • 10
  • 23
0

The changes in your User entity class may sometimes not reflect your DB schema accurately. You can try one of these below solutions:

1. Update your DB schema manually by adding AUTO_INCREMENT attribute

ALTER TABLE `user` CHANGE COLUMN `id` `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT;

2. Drop the User table in your DB, and rerun the application

Make sure that spring.jpa.hibernate.ddl-auto is set to create or update. The default is none if you are NOT using an embedded/in-memory DBs like H2 database.

This configuration of Spring Data JPA will set Hibernate's hibernate.hbm2ddl.auto to the setting value. In our case, it is create or update.

You can read more about this in the below articles and docs.
Spring Boot reference - Database Initialization
What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

In production, I suggest you not to use this option but instead use a database migration tool like Liquibase or Flyway and leave the spring.jpa.hibernate.ddl-auto configuration to be none.

More reads about this Hibernate: hbm2ddl.auto=update in production?

Long Nguyen
  • 179
  • 1
  • 6