My developer connection home My Developer Connection
knowledgebase for software developers

2. Simple Reference (many-to-one)

Definition: A many-to-one reference is analogous to a simple Java reference. It is the same as the one-to-one situation except there is no requirement to have a shared PK. Instead a FK is used.

Scenario: We have two first-rank classes, Foo and Bar which are related to each other as follows:

Bar Foo.getBar() // returns corresponding Bar instance

Hibernate Mapping:
In Hibernate, this could be mapped as follows:

<class name="Foo" table="foo">
     ...
     <many-to-one name="bar" class="Bar" column="bar_id"/>
</class>

Table Schema:

Foo

id

bar_id

Bar

id


Now we have created an extra column in Foo's table which holds the FK to Bar. Foo and Bar can have completely different PKs and the relationship will still hold.

Bidirectionality:
This relationship can be declared both ways, with Bar having getFoo(), by simply adding a similar mapping and property to Bar. This will result in Bar's table getting an extra column foo_id.

Send mail to webmaster@mydeveloperconnection.com with questions or comments about this web site.
  Send mail to webmaster@mydeveloperconnection.com with questions or comments about this web site.
  Send mail to webmaster@mydeveloperconnection.com with questions or comments about this web site.