One to one XML mapping with primary key association

In Hibernate we can achieve One to One relationship using XML Mapping or using annotations. This hibernate tutorial explains how two tables are linked with each other with the help of one to one mapping using XML Base Mapping approach.

One-to-One mapping can be done in two ways:

  1. Primary key association
  2. Foreign key association

This tutorial is based on primary key association with  both the tables share same primary key.

One to One Hbernate mapping

Let’s start with creating the bean classes first, the first being the in com.jCombat.bean package as:

Similarly, create class in com.jCombat.bean package as :

Create Student.hbm.xml in ‘src’ directory, for mapping Student bean to the database table as:

Create Address.hbm.xml in ‘src‘ folder for mapping Address bean to the database table as:

Create hibernate.cfg.xml in ‘src’ folder for configuring  database connection and mapping declarations as:

Create for initial hibernate configuration as:

Create as:

On execution, the following output gets displayed on console:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.