简体   繁体   中英

Spring boot error: NULL not allowed for column “ID” error

I keep getting this error on start up of my Spring Boot Application

Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into bookings(bookings_name) values('Kris') [23502-199]

data.sql has

insert into bookings(bookings_name) values('Kris');
insert into bookings(bookings_name) values('Martin');

And I believe I have the annotations so that @Id is generated automatically

@Entity
class Bookings {


    @Id
    @GeneratedValue
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;


    private String BookingsName;

    public Long  getId() {
        return id;
    }

    public String getBookingsName() {
        return BookingsName;
    }

    public Bookings(String BookingsName) {
        super();
        this.BookingsName = BookingsName;

    }


    @Override
    public String toString() {
        return "Bookings [id=" + id + ", BookingsName=" + BookingsName + "]";
    }

}

I am just starting to learn Spring boot and every example I find online doesn't seem to transalte to my very trivial example here.


Full Application.java

import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


@Component
class BookingsCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {



    }

}




interface BookingsRepository extends JpaRepository<Bookings, Long> {

    Collection<Bookings> findByBookingsName(String BookingsName);

}


@RestController
class BookingsRestController {

    @Autowired
    BookingsRepository BookingsRepository;

    @RequestMapping("/Bookingss")
    Collection<Bookings> Bookingss() {
        return this.BookingsRepository.findAll();

    }

}


@Entity
class Bookings {


    @Id
    @GeneratedValue
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;


    private String BookingsName;

    public Long  getId() {
        return id;
    }

    public String getBookingsName() {
        return BookingsName;
    }

    public Bookings(String BookingsName) {
        super();
        this.BookingsName = BookingsName;

    }


    @Override
    public String toString() {
        return "Bookings [id=" + id + ", BookingsName=" + BookingsName + "]";
    }

}

Which database do you use ? I think that the default strategy may not always work.
Try to use a different strategy in your entity declaration :

@Entity
class Bookings {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

I think there is a confusion here about Spring/JPA and DBMS ID generation. Running pure SQL to your database (through java code or not) like hardcoded "insert into bookings(bookings_name) values('Kris');" will not care about the Id field of the entity being annotated @GeneratedValue or not.

To insert the bookings you could :

  • Use the JPA way to insert data through EntityManager
  • configure your DBMS schema to generate default IDs
  • Use IDs in the insert statement of your SQL

Hope that could help,

bye

You need to alter your database so that the ID column is an identity/auto-increment column ie the value will be managed by H2 and clients will not specify a value for new records.

Once you have done this then the the records in data.sql should be loaded without any issues.

auto increment ID in H2 database

For the JPA entities you need to update as below to advise your JPA provider that IDs are manged by the database:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM