我正在使用 Hibernate 3 注释。我有一个具有一对多关系的表“产品”和子表“产品规格”。当我做 hibernateTemplate.save(product) 它给出错误
无法插入:[com.xx.ProductSpec];SQL [插入 Products_spec 列 'PRODUCT_ID' 不能为空
@Entity
@Table(name = "product")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
@Column(name = "PRODUCT_ID")
private Integer productId;
@Column(name = "PRODUCT_NAME")
private String productName;
@OneToMany(mappedBy = "product",fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<ProductSpec> specs = new ArrayList<ProductSpec>();
//getter and setter
}
@Entity
@Table(name = "Products_spec")
public class ProductSpec implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
@Column(name = "spec_id")
private Integer specId;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
private Product product;
//getter and setter
}
hibernateUtil.getTemplate().save(product);