phanhongphucit
phanhongphucit

Reputation: 547

The parentId do not set to the child entity automatically by cascade

In the entity Invoice, I have a set of DetailInvoice with cascade is ALL

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "invoice")
public Set<DetailInvoice> getDetailInvoices() {
    return detailInvoices;
}

In the entity DetailInvoice:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "invoice_id")
public Invoice getInvoice() {
    return invoice;
}

Saving:

    Invoice invoice = new Invoice();
    DetailInvoice detailInvoice = new DetailInvoice();
    invoice.getDetailInvoices().add(detailInvoice);
    detailInvoice.setInvoice(invoice); // (1) Should we need this row?
    session.save(invoice);

If we do not have a row (1), the detailInvoice will have invoiceId is null in database. Why Hibernate do not setInvoiceId to detailInvoice automatically base on the annotation mapping?

Many thanks

Upvotes: 0

Views: 1593

Answers (1)

SB.
SB.

Reputation: 1887

Please refer to

Hibernate 4.1 Documentation Section 1.2.6

The Person and Events example is something similar to what you are talking about.

Regards,

Shardul.

Upvotes: 1

Related Questions