在实体 Invoice 中,我有一组 DetailInvoice 与 cascade is ALL
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "invoice")
public Set<DetailInvoice> getDetailInvoices() {
return detailInvoices;
}
在实体 DetailInvoice 中:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "invoice_id")
public Invoice getInvoice() {
return invoice;
}
保存:
Invoice invoice = new Invoice();
DetailInvoice detailInvoice = new DetailInvoice();
invoice.getDetailInvoices().add(detailInvoice);
detailInvoice.setInvoice(invoice); // (1) Should we need this row?
session.save(invoice);
如果我们没有行 (1),detailInvoice 将在数据库中具有 invoiceId 为空。为什么 Hibernate 不根据注解映射自动将 InvoiceId 设置为 detailInvoice?
非常感谢