【发布时间】:2021-05-28 19:00:56
【问题描述】:
我有两个实体:Product 和 Category
public class Category {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int id;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "category")
private Set<Product> products;
}
public class Product {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int id;
@ManyToOne
@JoinColumn(name = "categories_id")
private Category category;
}
我想在删除父级(类别)后将我的子级(产品)保留在 db 中(category 的值等于 null)。
目前,当我尝试删除一个类别时,我收到此错误:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "CONSTRAINT_F2: PUBLIC.PRODUCTS FOREIGN KEY(CATEGORIES_ID) REFERENCES PUBLIC.CATEGORIES(ID)
【问题讨论】:
标签: spring-boot spring-data-jpa foreign-keys sql-delete