Javatpoint Logo
Javatpoint Logo

Hibernate One to One Example using XML

There are two ways to perform one to one mapping in hibernate:

  • By many-to-one element (using unique="true" attribute)
  • By one-to-one element

Here, we are going to perform one to one mapping by one-to-one element. In such case, no foreign key is created in the primary table.

In this example, one employee can have one address and one address belongs to one employee only. Here, we are using bidirectional association. Let's look at the persistent classes.

1) Persistent classes for one to one mapping

There are two persistent classes and Employee class contains Address class reference and vice versa.

2) Mapping files for the persistent classes

The two mapping files are employee.hbm.xml and address.hbm.xml.


In this mapping file we are using one-to-one element in both the mapping files to make the one to one mapping.


This is the simple mapping file for the Address class. But the important thing is generator class. Here, we are using foreign generator class that depends on the Employee class primary key.

3) Configuration file

This file contains information about the database and mapping file.


4) User classes to store and fetch the data


Hibernate One to One Example 1 Hibernate One to One Example 2


Hibernate One to One Example 3


Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA