春季启动错误:列“ ID”错误不允许为NULL

时间:2019-07-16 17:30:51

标签: java spring spring-boot spring-data-jpa

在我的Spring Boot应用程序启动时,我总是收到此错误

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具有

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

我相信我有注释,以便自动生成@Id

@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 + "]";
    }

}

我刚刚开始学习Spring引导,在网上找到的每个示例似乎都无法转化为我这里的琐碎示例。


完整的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 + "]";
    }

}

3 个答案:

答案 0 :(得分:0)

您使用哪个数据库?我认为默认策略可能并不总是有效。
尝试在实体声明中使用其他策略:

@Entity
class Bookings {

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

答案 1 :(得分:0)

我认为关于Spring / JPA和DBMS ID的生成这里有些混乱。对纯数据库运行SQL(无论是否通过Java代码),就像硬编码的“插入预订(bookings_name)值('Kris');”中一样不会在乎是否将实体的ID字段注释为@GeneratedValue。

要插入预订,您可以:

  • 使用JPA方法通过EntityManager插入数据
  • 配置DBMS架构以生成默认ID
  • 在SQL的插入语句中使用ID

希望有帮助,

再见

答案 2 :(得分:0)

您需要更改数据库,以使ID列为标识/自动递增列,即该值将由H2管理,并且客户端将不会为新记录指定值。

完成此操作后,应立即加载data.sql中的记录。

auto increment ID in H2 database

对于JPA实体,您需要进行以下更新,以告知您的JPA提供者ID由数据库管理:

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