One-to-one Mapping in Hibernate by R4R Team

One to One association is represent that another persistent class is declared using a one to one element.


<one-to-one 

name="propertyName"  

class="ClassName"  

cascade="cascade_style"  

constrained="true|false"  

fetch="join|select"  

property-ref="propertyNameFromAssociatedClass"  

access="field|property|ClassName"  

formula="any SQL expression"  

lazy="proxy|no-proxy|false"  

entity-name="EntityName"  

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

/>


Property

Description

name

It shows the name of the property.

class(optional)

We found this by default to the property type determined by reflection.

This tells us the name of the associaited class.

cascade(optional)

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

constrained(optional)

It specified that a foreign key constraint on the primary key of the mapped table and references the table of that associated class. In this option we found the affects the order in which save() and delete() are cascaded, and determines whether the association can be proxyied. It is also used by the schema export tool.

fetch(optional)

We found this by defaults to select.

We need to choose between outer-join fetching or sequetial select fetching.

property-ref(optional)

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

access(optional)

We found this by default on the property.

The strategy in hibernated uses for accessing the property value.

formula(optional)

It almost all one-to-one association map to the priamry key of the owning entity. If this is not the case,that can specify another column, column or expression to join on using an SQL formula.

lazy(optional)

It set by defaults to proxy.

In single point association are proxied. lazy=”no-proxy” specifies that the property should be fetched lazily when the instance variable is first accessed. It requires build-time bytecode instrumentation. lazy=”false” specifies that the association will always be eagerly fetched. We need to remember is that if constrained=”false”, then proxying is impossible and hibernate will eagerly fetch the association.

entity-name(optional)

It represent the entity name of the associated class.


In hibernate we found the two varieties of one-to-one associations that is given below:

1. Primary key associations
2. Unique foreign key associations

In the one to one association we don't need to add extra table column. when if two rows are related by the association then two table rows share primary key value. Two object by a primary key association relate to ensure that they are assigned the same identifier value.

For a primary key association, add the below mappings code to Employee and Person respectively:

<one-to-one name="person" class="Person"/>
<one-to-one name="employee" class="Employee" constrained="true"/>

Ensure that the primary keys of the related rows in the PERSON and EMPLOYEE tables are equal.

for calling the foreign key strategy we need to use special Hibernate identifier generation strategy:

<class name="person" table="PERSON"> 
<id name="id" column="PERSON_ID"> 
<generator class="foreign"> 
<param name="property">employee</param> 
</generator> </id> ... <one-to-one name="employee" class="Employee" constrained="true"/> 
</class>

A newly saved instance of Person is assigned the same primary key value as the Employee instance referred with the employee property of that Person.

Foreign key with a unique constraint, from Employee to Person, can be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>

This association can be made bidirectional by adding the following to the Person mapping:

<one-to-one name="employee" class="Employee" property-ref="person"/>
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!