0

在实体 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?

非常感谢

4

1 回答 1

1

请参阅

Hibernate 4.1 文档第 1.2.6 节

人员和事件示例与您正在谈论的内容类似。

问候,

沙杜尔。

于 2012-04-12T05:29:56.267 回答