Hibernate Handling Associations

There are different associations that hibernate handles. These are explained in details below.

One to One Association

One to one association is done by using two keys.

  1. Primary key association
  2. Foreign key association

Association mapping of one to one is done in different methods.

  1. Unidirectional association
  2. Unidirectional association with join tables
  3. Bidirectional association
  4. Bidirectional association with join tables
Hibernate Handling Associations
Hibernate Handling Associations

1)      Unidirectional associations

1.1)  One to one unidirectional association on a primary key used a special id generator.

Eg: –

<class>
<id column="personId">
<generator/>
</id>
</class>
<class>
<id column="personId">
<generator>
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class>

1.2)  One to one unidirectional association on a foreign key is identical and the difference is the column unique constraint.

Eg: –

class>
<id column="personId">
<generator/>
</id>
<many-to-one column="addressId” unique="true"
not-null="true"/>
</class>
<class>
<id column="addressId">
<generator class="native"/>
</id>
</class>

2)      A unidirectional association with join tables. An example of unidirectional associations with join table is given below.

Eg: –

<class>
<id column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress" optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address" column="addressId” not-null="true"
unique="true"/>
</join>
</class>
<class>
<id column="addressId">
<generator/>
</id>
</class>

3)      Bidirectional association

3.1) In the bidirectional one to one association on a primary key also uses the special id generator.

Eg: –

 <class>
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class>
<class>
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-onetrue"/>
</class>

3.2)    Here we can also use foreign key concept.

Eg: –

 <class>
<id column="personId">
<generator/>
</id>
<many-to-one column="addressId" unique="true"
not-null="true"/>
</class>
<class>
<id column="addressId">
<generator/>
</id>
<one-to-one property-ref="address"/>
</class>

4)      Bidirectional associations with join tables. This operation is also possible.

Eg: –

 <class name="Person">
<id column="personId">
<generator/>
</id>
<join table="PersonAddress" optional="true">
<key column="personId” unique="true"/>
<many-to-one column="addressId" not-null="true “unique="true"/>
</join>
</class>
<class>
<id column="addressId">
<generator/>
</id>
<join table="PersonAddress” optional="true" inverse="true">
<key column="addressId" unique="true"/>
<many-to-one column="personId" not-null="true” unique="true"/>
</join>
</class>

Many to One Association

Our association is one way we can access the countries listed continent from an object, but cannot access directly to the mainland possession of a country. We bidirectional association by changing the kind of country and add a reference to one of many around the continent.

An ordinary association to another persistent class is declared using an element of many-to-one. The relational model is an association many-to-one: a foreign key in a table references the primary key column (s) of the target table.

<many-to-one name="propertyName"
column="column_name"
class="ClassName"
cascade="cascade_style"
fetch="join|select"
update="true|false"
insert="true|false"
property-ref="propertyNameFromAssociatedClass"
access="field|property|ClassName"
unique="true|false"
not-null="true|false"
optimistic-lock="true|false"
lazy="true|false"
entity-name="EntityName"/>

(1) name: The name of the property.

(2) column (optional): The name of the foreign key column. This may also be specified by nested <column> element(s).

(3) class (optional – defaults to the property type determined by reflection): The name of the associated class.

(4) cascade (optional): Specifies which operations should be cascaded from the parent object to the associated object.

(5) join (optional – defaults to select): Chooses between outer-join fetching or sequential select fetching.

(6) update, insert (optional – defaults to true) specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure “derived” association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.

(7) property-ref: (optional):- The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

(8) access (optional – defaults to property): The strategy Hibernate should use for accessing the property value.

(9) unique (optional): Enable the DDL generation of a unique constraint for the foreign-key column. Also, allow this to be the target of a property-ref. This makes the association multiplicity effectively one to one.

(10) not-null (optional): Enable the DDL generation of a null ability constraint for the foreign key columns.

(11) optimistic-lock (optional – defaults to true): Specifies that updates to this property do or do not require acquisition of the optimistic lock. In other words, determines if a version increment should occur when this property is dirty.

(12) lazy (optional – defaults to false): Specifies that this property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation).

A typical many-to-one declaration looks as simple as this:

<many-to-one name=”product” column=”PRODUCT_ID”/>

Association mapping of many to one is done in different methods. They are,

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

 

1)      It’s a common unidirectional association.

Eg: –

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

2)      A unidirectional association with join tables. When association is optional we are using unidirectional many to one association on join table.

Eg: –

<class>
<id column="personId">
<generator/>
</id>
<join table="PersonAddress"  optional="true">
<key column="personId" unique="true"/>
<many-to-one column="addressId" not-null="true"/>
</join>
</class>
<class>
<id column="addressId">
<generator/>
</id>
</class>

3)      Bidirectional association. This is a common association.

Eg: –

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

4)      Bidirectional associations with join tables. This operation is also possible in this mechanism.

Eg: –

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