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.


<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="proxy|no-proxy|false"  

not-found="ignore|exception"  

entity-name="EntityName"  

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" 

/>


Property

Description

name(optional)

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

class(optional)

It defaults set on the property type determined by reflection.

The name of the associated class.

cascade(optional)

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

fetch(optional)

Defaults set on select.

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

update,insert(optional)

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.

property-ref(optional)

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.

access(optional)

We found it by defaults on property.

The strategy hibernae uses for accessing the property value.

unique(optional)

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.

not-null(optional)

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

optimistic-lock(optional)

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.

lazy(optional)

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.

Not-found(optional)

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.

entity-name(optional)

This entity name of the associated class.

formula(optional)

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:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in 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!