hibernate relationship

Posted: April 19, 2012 in Hibernate

Introduction To Relationships

Foo

The basic Foo looks like this:

Public class Foo

{

Protected int id;

Public Foo()

{

}

Public void setId(int id)

{

}

Public int getId()

{

}

}

Foo’s basic mapping looks like this:

<hibernate-mapping>

<class name=”Foo” table=”foo”>

<id name=”id” column=”id” type=”int”>

<generator class=”assigned”/>

</id>

</class>

</hibernate-mapping>

Bar

The basic bar looks like this

Public class Bar

{

Protected int id;

Public Bar()

{}

Public void setId(int id)

{

}

Public int getId()

{

}

}

Bar’s basic mapping look like this

<hibernate-mapping>

<class name=”Bar” table=”bar”>

<id name=”id” column=”id” type=”int”>

<generator class=”assigned”/>

</id>

</class>

</hibernate-mapping>

For both Foo and Bar are choosing to an assigned id generator. There is nothing special about this choice. We are simply choosing to program our ids manually.

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 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 association.

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

Bi directionality:

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

2.       Simple Reference (many-to-one)

Definition:

A many-to-one reference is analogous to a simple Java reference. It is as the one-to-one situation except there is no requirement to have a shred 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.

Bi directionality:

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

3.       Basic Collection (one-to-many)

Definition:

A one-to-many reference is basically a collection. Here A first-rank class, a holds a reference to a collection of another first-rank class, B.

Scenario:

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

Set Foo.getBars()    // returns collection of Bar instances

Hibernate Mapping:

In hibernate, this could be mapped as follows:

<class name=”Foo” table=”foo”>

…….

<set role=”bars” table=”bar”>

    <key column=”foo_id”/>

   <one-to-many class=”Bar”/>

</set>

</class>

We will mostly use <set> type collections in these examples except where we are examining specific collection-type features. Please refer to the hibernate documentation to learn about the comprehensive set of collection types available.

Table Schema:

Foo
Id
Bar
Id foo_id

Now we have created an extra column in Bar’s table which holds the FK to Foo. This allows Foo to be assigned a Collection of Bar’s based on the value of the foo_id column in Bar.

Bi directionality:

This relationship can be declared both ways, with Bar having getFoo(), by suitable code changes to Bar and the following schema change:

<class name=”Bar” table=”bar”>

……

<many-to-one name=”foo” class=”Foo” column=”foo_id”/>

</class>

Now your Bar’s will know who their Foo is.

Extra columns are generated for the bi directionality.

4.       Collection (many-to-many)

Definition:

A many-to-many reference is basically a collection. First-rank class A holds a reference to a set of first-rank class B instances (as in the one-to-many case), but B might have multiple A’s.

Scenario:

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

Set Foo.getBars()   //returns a collection of Bar instances

Hibernate Mapping:

<class name=”Foo” table=”foo”>

….

<set role=”bars” table=”foo_bar”>

<key column=”foo_id”/>

<many-to-many column=”bar_id” class=”Bar”/>

</class>

Table Schema:

Foo
Id
Bar
Id
Foo_Bar
foo_id bar_id

This time we cannot have an extra column on Bar as that would dictate that each Bar has only one Foo.  So instead we have an extra table, foo_bar, which holds the relationship between

Bi directionality:

This relationship can be declared both ways, with Bar having getFoos(), by suitable code changes to Bar and the following schema change:

<class name=”Bar” table=”bar”>

…….

<set role=”foos” table=”foo_bar” read-only=”true”>

<key column=”bar_id”/>

<many-to-many column=”foo_id” class=”Foo”/>

</set>

</class>

Now your Bar’s will know who their Foo’s are

No extra columns are generated for bi directionality.

Note that one end of the relationship must be declared “readonly”.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s