Hibernate One to Many and Many to Many Associations

This section describes the different associations in hibernate.They are basically one to many and many to many associations.

One to Many Association

One-to-many relationships occur when each record in Table A (Author) may have many linked records in Table B (Books) but each record in Table B may have only one corresponding record in Table A.

One to Many Association
One to Many Association

One to many provides four different methods to handling hibernate Association. They are,

  1. Unidirectional association
  2. Unidirectional association with join tables
  3. Bidirectional association
  4. Bidirectional association with join tables

1)      Unidirectional association

Hibernate one to many association provide this mechanism by using foreign key.

Eg: –

<class>
<id column="personId">
<generator/>
</id>
<set>
<key column="personId” not-null="true"/>
<one-to-many class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator/>
</id>
</class>

2)      Unidirectional association with join tables

A unidirectional one-to-many join table is a preferred option. Specifying unique = “true”, changed the multiplicity from many to many one-to-many.

Eg: –

 <class name="Person">
<id column="personId">
<generator/>
</id>
<set table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId" unique="true" class="Address"/>
</set>
</class>
<class>
<id column="addressId">
<generator/>
</id>
</class>

3)      Bidirectional association

This association is most common type of association.

Eg: –

 <class name="Person">
<id column="personId">
<generator/>
</id>
<many-to-one column="addressId" not-null="true"/>
</class>
<class>
<id column="addressId">
<generator/>
</id>
<set inverse="true">
<key column="addressId"/>
<one-to-many/>
</set>
</class>

4)      Bidirectional association with join tables

The following is an example of a bidirectional one-to-many association on a join table.

Eg:-

<class>
<id column="personId">
<generator/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId" unique="true" class="Address"/>
</set>
</class>
<class>
<id column="addressId">
<generator/>
</id>
<join table="PersonAddress” inverse="true"    optional="true">
<key column="addressId"/>
<many-to-onepersonId" not-null="true"/>
</join>
</class>

Many to Many Association

Many to many relationships is a type of cardinality. It refers to the relationship between two entities. For example in the below figure A and B are two entities in which a may contain a parent row for which there are many children in B and vice versa. In the figure A represented as Authors and B represented as Books. An Author can write several Books and a Book can be written by several Authors.

Many to Many Association
Many to Many Association

A logical data relationship in which the value of one data element can exist in the combination with many values of another data element, and vice versa. Many to many association support only join table mechanism.  That is it support only two methods for handling many to many hibernate kind association. They are,

1)      Unidirectional association with join tables

Here we are discussing the example of many to many unidirectional associations.

Eg:-

<class name="Person">
<id column="personId">
<generator class="native"/>
</id>
<set table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"/>
</set>
</class>
<class>
<id column="addressId">
<generator/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )

2)      Bidirectional association with join tables

The following sample program describes the working of many to many bidirectional associations with join tables.

Eg:-

<class>
<id column="personId">
<generator/>
</id>
<set table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId" class="Address"/>
</set>
</class>
<class>
<id column="addressId">
<generator/>
</id>
<set inverse="true" table="PersonAddress">
<key column="addressId"/>
<many-to-many column="personId"/>
</set>
</class>

create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )