aberrant80
aberrant80

Reputation: 13017

Hibernate - why use many-to-one to represent a one-to-one?

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.

Upvotes: 51

Views: 31579

Answers (5)

baHI
baHI

Reputation: 1580

The biggest difference is, with a shared-key one-to-one mapping the 2 objects are bound to each other, they exists together.

f.e. if you create a Person and an Address class that are bound to tables with same name, each person will have exactly one address...

  • class Person -> properties: address
  • table Person -> columns: id, name
  • table Address -> columns: id, city

With many-to one relationship the table structure changes a bit, but the same effect can be achieved...

  • class Person -> properties: address
  • table Person -> columns: id, name, addressid (fk)
  • table Address -> columns: id, city

...but even more. Now this person can have multiple addresses:

  • class Person -> properties: address
  • table Person -> columns: id, name, addressid (fk), shippingaddressid (fk)
  • table Address -> columns: id, city

The two foreign keys (addressid and shippingaddressid) could point to a single DB entry...or a single address could belong to 2-3 persons. so whats a many-to-one from the person's side it's a one-to-many from the address side.

and just guess what does a one-to-many association with only 1 item look like? Yeah, just like a one-to-one...

NOTE: address actually should be a value object, should not be shared in DB (so it's a silly example, but i guess it'll be o.k.)

So in short:

  1. in OR mapping one-to-one is harder to handle
  2. one to one has it's limitations
  3. using many to one instead is more flexible and same thing can be achieved with

Upvotes: 7

DanDare
DanDare

Reputation: 1

As I understand it hibernate requires that the primary key of both objects match in a 1 to 1 relationship. Many to 1 avoids that requirement.

However many to 1 loses the information that there should only be one or perhaps no object on the many side.

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570595

There are several ways to implement a one-to-one association in a database: you can share a primary key but you can also use a foreign key relationship with a unique constraint (one table has a foreign key column that references the primary key of the associated table).

In the later case, the hibernate way to map this is to use a many-to-one association (that allows to specify the foreign key).

The reason is simple: 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. All you want is to express “This entity has a property that is a reference to an instance of another entity” and use a foreign key field to represent that relationship.

In other words, using a many-to-one is the way to map one-to-one foreign key associations (which are actually maybe more frequent than shared primary key one-to-one associations).

Upvotes: 38

Tomislav Nakic-Alfirevic
Tomislav Nakic-Alfirevic

Reputation: 10173

I would say the problem is fundamentally related to the object-relational impedance mismatch. To be able to relate the two object representations in a database, you need to have some sort of relationship between their tables. However, the database knows only the 1:N relationship: all the others are derived from it.

With relational databases and object languages, it's up to the developer to find the least unnatural representation of the concept he/she wants to represent (in this case, a 1:1 relationship).

Upvotes: 3

Konrad Garus
Konrad Garus

Reputation: 54045

It's even in official Hibernate docs: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/associations.html#assoc-bidirectional-121.

It's not totally unreasonable. The many-to-one end says: I am mapped via one of my columns to an ID of the -one end. You would use the exact same database schema for many-to-one.

Upvotes: 0

Related Questions