Sunday , June 25 2017
Home / Hibernate / Hibernate One to One Mapping using primary key (XML)

Hibernate One to One Mapping using primary key (XML)

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 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 foreign key, then we can call it as one to one relationship using primary key.

For bettor 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 :

pom.xml


<dependencies>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <!-- MySQL Driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.5</version>
        </dependency>
    </dependencies>

Hibernate POJO Classes :

Person.java


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;
    }
    
}

Passport.java


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 :

person.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.onlinetutorialspoint.hibernate.pojo.Person" table="person">
        <id name="personId" column="personid" />
        <property name="personName" column="personname" />
    </class>
</hibernate-mapping>

passport.hbm.xml


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

To represent the one to one relationship using 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 :

HibernateUtil.java


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()
                    .applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        }
        return sessionFactory;

    }
}

Lets run the application :

Main.java

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.setPersonId(1002);
        person.setPersonName("ChandraShekhar Goka");

        Passport passport = new Passport();
        passport.setPassportNumber(852963147);
        passport.setExpireDate(new Date());
        passport.setIssudDate(new Date());
        passport.setPerson(person);

        Transaction transaction = session.beginTransaction();
        session.save(passport);
        transaction.commit();

        session.close();
        sessionFactory.close();
    }
}

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

 

Download Example

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Hibernate Native SQL Query Example

In this tutorials, we are going to learn about Hibernate Native SQL with example. Hibernate …

Leave a Reply

Your email address will not be published. Required fields are marked *