Reputation: 7162
I am trying to make a small invoicing system using Spring, Hibernate, so I built three domain classes (Product), (InvoiceMaster) and (InvoiceDetails), now I am a bit confused about the relationship between (InvoiceDetails) and (Product), in (InvoiceDetails) I have added products as:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCT_ID", nullable=false)
private Product product;
but then I found many online examples which describe the relation as @OneToOne so I am confused should the relation be as @OneToOne or @ManyToOne?!
From my understanding it is many invoice details records for one product, so it should be Many to One, please advice
Thanks for your time
Upvotes: 0
Views: 51
Reputation: 9162
If one Product
can be used in several InvoiceDetails
, you should use @ManyToOne
, otherwise use @OneToOne
.
Just from my own experience, @ManyToOne
is more flexible than @OneToOne
in terms of queries optimization.
Upvotes: 1
Reputation: 691635
OneToOne means: a product only has one (or zero) invoice detail, and an invoice detail concerns one (or zero) product.
My guess is that you can have several invoice details for the same product (because you'll sell many copies of a product to various customers), so the association should be a ManyToOne.
Upvotes: 1