我的班级结构如下:
Product.java
@Entity
@Table(name = "products")
@Getter @Setter
public class Product extends BaseEntity {
@Column(name = "name", nullable = false)
protected @NotEmpty String name;
@Column(name = "is_active")
protected Boolean isActive = false;
@Column(name = "price")
protected @NotNull Integer price;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "product")
protected ProductDetails productDetails;
}
ProductDetails.java
@Entity
@Getter @Setter
public class ProductDetails extends BaseEntity {
@Column(name = "description")
protected String description;
@Column(name = "cuisines")
protected String cuisines;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "product_id")
protected Product product;
}
存储库同时为product和productDetails实现CrudReporitory。 我的POST请求是:
{
"isActive": true,
"name": "product4",
"price": 10,
"productDetails" : {
"description": "prod desc 4",
"cuisines": "south, asian"
}
}
发送上述请求后, 插入产品(created_at,updated_at,is_active,名称,价格)值(?,?,?,?,?) 并将其插入到product_details(created_at,updated_at,美食,说明,product_id)值(?,?,?,?,?)
被执行,但是在查询2的情况下product_id为null
,这给我带来的错误为SQLIntegrityConstraintViolationException: Column 'product_id' cannot be null
我该如何解决?