Hibernate - why use many-to-one to represent a one-to-one?
Posted
by aberrant80
on Stack Overflow
See other posts from Stack Overflow
or by aberrant80
Published on 2010-03-16T08:42:59Z
Indexed on
2010/03/16
8:46 UTC
Read the original article
Hit count: 202
hibernate
I've seen people use many-to-one mappings to represent one-to-one relationships. I've also read this in a book by Gavin King and on articles.
For example, if a customer can have exactly one shipping address, and a shipping address can belong to only one customer, the mapping is given as:
<class name="Customer" table="CUSTOMERS">
...
<many-to-one name="shippingAddress"
class="Address"
column="SHIPPING_ADDRESS_ID"
cascade="save-update"
unique="true"/>
...
</class>
The book reasons as (quoting it):
"You don't care what's on the target side of the association, so you can treat it like a to-one association without the many part."
My question is, why use many-to-one
and not one-to-one
? What is it about a one-to-one
that makes it a less desirable option to many-to-one
?
Thanks.
© Stack Overflow or respective owner