Many-to-one mapping in Hibernate by R4R Team

In the Hibernate we found an ordinary association to another persistent class is declared using a many to many element. In this relational model we used a many to many association. In this association we use a foreign key is one table is referencing the primary key column of the target table.

















formula="arbitrary SQL expression"  

node="element-name|@attribute-name|element/@attribute|." embed-xml="true|false" index="index_name" unique_key="unique_key_id" foreign-key="foreign_key_name" 





Here we use the foreign key column. This can also be specified by nested <column> element.


It defaults set on the property type determined by reflection.

The name of the associated class.


It specifies which operations should be cascaded from the parent object to the associated object.


Defaults set on select.

Here we need to chooses between outer-join fetching or sequential select fetching.


It sets on by default on the true.

It sepecifies that the mapped columns should be included in SQL UPDATE and INSERT satatement. Setting both to false allows a pure “derived” association whose value is initialized from another property that maps to the same column or by a trigger or other application.


The name of a property of the associated class that is joined to this foregin key. If not specified, the primary key of the associated class is used.


We found it by defaults on property.

The strategy hibernae uses for accessing the property value.


It enables the DDL generations of a unique constraint for the foregin-key column. By allowing this to be the target of a property-ref, with this we can make the association multiplicity one to one.


It enables the DDL generation of a nullability constraint for the foreign key columns.


It defaults to true.

It sepcifies that updates to this property do or do not require acquistion of the optimistic lock. In other word we can it determines if a version increment should occur when this property is dirty.


Defaults on proxy.

We found by defaults, single point associations are proxied. lazy=”no-proxy” specifies that property should be fetched lazily when the instance variable is first accessed. This requires build time bytecode instrumentation. lazy=”false” specifies that the association will always be eagerly fetched.


We found this by defaults to exception.

It specifies how foreign key that reference missing rows will be handled. So we need to ignore will treat a missing row as a null association.


This entity name of the associated class.


An SQL exception that defines the value for a computed foreign key.

In the hibernate we set a value of the cascade attribute to any meaningful value other then non will propagate certain operations to the associated object. The meaning full values are divided into three categories.

1. Basic operaton which include: persist, merge, delete, save-update, evict, replicate, lock and refresh;
2. Special values: delete-orphan;
3. All comma-separated combinations of operation names: cascade="persist,merge,evict" or cascade="all,delete-orphan". See Section 10.11, “Transitive persistence” for a full explanation. Note that single valued, many-to-one and one-to-one, associations do not support orphan delete.

Here is an example of a typical many-to-one declaration:

<many-to-one name="product" class="Product" column="PRODUCT_ID"/>

The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is a complicated and confusing relational model. 

Example, if the Product class had a unique serial number that is not the primary key. The unique attribute controls Hibernate's DDL generation with the SchemaExport tool.

<property name="serialNumber" unique="true" type="string" column="SERIAL_NUMBER"/>

Then the mapping for OrderItem might use:

<many-to-one name="product" property-ref="serialNumber" column="PRODUCT_SERIAL_NUMBER"/>

This is not encouraged, If the referenced unique key comprises multiple properties of the associated entity, you should map the referenced properties inside a named <properties> element.

If the referenced unique key is the property of a component, you can specify a property path:

<many-to-one name="owner" property-ref="identity.ssn" column="OWNER_SSN"/>
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!