Next Previous Index

1. Simple Association (one-to-one)


Definition:

Our first relationship is a simple association. In this relationship, one first-rank class holds a reference to a single instance of another first-rank class and they are related by a common PK.


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"
    ...
    <one-to-one name="bar" class="Bar"/>
</class>

Table Schema:

Foo

id

Bar

id


No extra columns are needed to support this relationship. Instead both Foo and Bar must share the same PK values to be part of a one-to-one assocation.


If you create suitable instances of Foo and Bar with a shared PK, then retrieving a Foo will automatically retrieve the corresponding Bar.


Bidirectionality:

This relationship can be bidrectional, with Bar having getFoo(), by simply adding a similar mapping and Foo property to Bar.



Next Previous Index