小编dev*_*rko的帖子

错误:关系xxx的列“id”中的空值违反了非空约束 - Spring Data JPA

我有一个关于 Spring JPA 的 @GenerateValue 注释的问题。

这是我的课:

@Entity
@NoArgsConstructor
@Getter
@Setter
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private Double price;
    private Integer quantity;

    @ManyToOne
    private Category category;

    @ManyToOne
    private Manufacturer manufacturer;



    public Product(String name, Double price, Integer quantity, Category category, Manufacturer manufacturer) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.category = category;
        this.manufacturer = manufacturer;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试添加新产品时,出现此错误:

2021-12-06 18:03:02.013 ERROR 3720 --- [nio-9090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value …
Run Code Online (Sandbox Code Playgroud)

java postgresql spring-data spring-data-jpa spring-boot

4
推荐指数
1
解决办法
1万
查看次数