In the previous tutorials, we have implemented one to one relation in hibernate using foreign key. As we mentioned in the previous tutorial, we can implement the hibernate one to one mapping in two different ways. In this tutorial, we are going to implement the second type hibernate one to one mapping using the primary key.

One to one relationship is applicable if a primary key value of the parent table and primary key of child record both are the same. That is a single column of a child table acts as a primary key and also as a foreign key, then we can call it as one to one relationship using the primary key.

For better understanding, we are taking the same previous scenario like Person and Passport. Here the relation between the Person and Passport is one to one. Because one Person can have only one Passport.

Lets start the example for one to one mapping using primary key in hibernate:

Hibernate One to One Mapping using Primary Key :

Project Structure :

Hibernate One to One Mapping Project

Required Dependencies :

<!-- Hibernate -->
<!-- MySQL Driver -->

Hibernate POJO Classes :
package com.onlinetutorialspoint.hibernate.pojo;

public class Person {
private int personId;
private String personName;
public int getPersonId() {
return personId;
public void setPersonId(int personId) {
this.personId = personId;
public String getPersonName() {
return personName;
public void setPersonName(String personName) {
this.personName = personName;

package com.onlinetutorialspoint.hibernate.pojo;

import java.util.Date;

public class Passport {
private int passportNumber;
private Date issudDate;
private Date expireDate;
private Person person;
public Person getPerson() {
return person;
public void setPerson(Person person) {
this.person = person;
public int getPassportNumber() {
return passportNumber;
public void setPassportNumber(int passportNumber) {
this.passportNumber = passportNumber;
public Date getIssudDate() {
return issudDate;
public void setIssudDate(Date issudDate) {
this.issudDate = issudDate;
public Date getExpireDate() {
return expireDate;
public void setExpireDate(Date expireDate) {
this.expireDate = expireDate;


Hibernate Mapping Files :


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.onlinetutorialspoint.hibernate.pojo.Person" table="person">
<id name="personId" column="personid" />
<property name="personName" column="personname" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.onlinetutorialspoint.hibernate.pojo.Passport"
<id name="passportNumber" column="ppid">
<generator class="foreign">
<param name="property">person</param>
<property name="issudDate" column="idate" />
<property name="expireDate" column="edate" />
<one-to-one name="person"
class="com.onlinetutorialspoint.hibernate.pojo.Person" cascade="all" />

To represent the one to one relationship using the primary key, we just use the
foreign generator in child table configuration.
The only usage of foreign generator in hibernate is, to represent the one to one relation using primary key.

Hibernate Utility Class :
package com.onlinetutorialspoint.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private HibernateUtil() {


private static SessionFactory sessionFactory;

public static synchronized SessionFactory getInstnce() {

if (sessionFactory == null) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
sessionFactory = configuration.buildSessionFactory(;
return sessionFactory;


Lets run the application :
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.onlinetutorialspoint.hibernate.pojo.Passport;
import com.onlinetutorialspoint.hibernate.pojo.Person;
import com.onlinetutorialspoint.util.HibernateUtil;

public class Main {

public static void main(String[] args) {

SessionFactory sessionFactory = HibernateUtil.getInstnce();
Session session = sessionFactory.openSession();

Person person = new Person();
person.setPersonName("ChandraShekhar Goka");

Passport passport = new Passport();
passport.setExpireDate(new Date());
passport.setIssudDate(new Date());

Transaction transaction = session.beginTransaction();;


Output :

Hibernate: select person_.personid, person_.personname as personna2_1_ from person person_ where person_.personid=?
Hibernate: insert into passport (idate, edate, ppid) values (?, ?, ?)
Hibernate: insert into person (personname, personid) values (?, ?)

Database Output :

Hibernate One to One Mapping output

Happy Learning 🙂

Download Example