2
我使用Hibernate 3註釋。 我有一個表'產品'和子表'product_spec'與一對多的關係。 當我做hibernateTemplate.save(產品)它給錯誤一對多級聯所有沒有設置父項ID,而子項插入
無法插入:[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);