Hibernate one to one mapping example

This is 3 of 8 parts of tutorial series

Tutorial Content:

    Introduction to hibernate framework Hibernate hello world example in eclipse Difference between openSession and getCurrentSession Hibernate one to one mapping example Hibernate one to many mapping example Hibernate many to many mapping example Hibernate inheritance:Table per class hierarchy Hibernate inheritance:table per subclass Hibernate inheritance:Table per concrete class Difference between openSession and getCurrentSession Difference between get and load Spring MVC Hibernate MySQL CRUD example Spring Rest hibernate example
In this example, we will see how to implement one to one relationship using annotations.
Lets take example of Country and Capital.One Country has one capital.Following is relationship diagram among them.

Now to create above tables in database, you need to create two java files i.e. Country.java and Capital.java.


Country class will be used to create COUNTRY table in database.
Create Country.java in src->org.arpit.javapostsforlearning.

package org.arpit.javapostsforlearning;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

public class Country {
    String countryName ;
    Capital capital;
    long countryPopulation;
    public Country()
    public Country(String countryName, long countryPopulation) {
  this.countryName = countryName;
  this.countryPopulation = countryPopulation;

    public long getCountryPopulation() {
  return countryPopulation;

    public void setCountryPopulation(long countryPopulation) {
  this.countryPopulation = countryPopulation;

    public String getCountryName() {
        return countryName;
    public void setCountryName(String countryName) {
        this.countryName = countryName;

    public Capital getCapital() {
  return capital;

    public void setCapital(Capital capital) {
  this.capital = capital;
@OneToOne annotation is used to create one to one relationship between Country and Capital entities.
 is used to specify a mapped column for joining an entity association.


Capital class will be used to create CAPITAL table in database.
Create Capital.java in src->org.arpit.javapostsforlearning.

package org.arpit.javapostsforlearning;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class Capital {

 String capitalName;
 long capitalPopulation;

 public Capital()
 public Capital(String capitalName, long capitalPopulation) {
  this.capitalName = capitalName;
  this.capitalPopulation = capitalPopulation;

 public String getCapitalName() {
  return capitalName;

 public void setCapitalName(String capitalName) {
  this.capitalName = capitalName;
 public long getCapitalPopulation() {
  return capitalPopulation;

 public void setCapitalPopulation(long capitalPopulation) {
  this.capitalPopulation = capitalPopulation;


Create a file named "hibernate.cfg.xml" in src folder.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;database=UserInfo</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

  <mapping class="org.arpit.javapostsforlearning.Country"></mapping>
  <mapping class="org.arpit.javapostsforlearning.Capital"></mapping>



4.Main Class:

package org.arpit.javapostsforlearning;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateMain {

public static void main(String[] args) {

Configuration configuration=new Configuration();
ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sf=configuration.buildSessionFactory(sr);
Session ss=sf.openSession();

Country countryIndia=new Country("India",50000000);
Capital capitalDelhi=new Capital("Delhi",4000000);
Country countryFrance=new Country("France",20000000);
Capital capitalParis=new Capital("Paris",1000000);



Project Struture:


5.SQL output:

COUNTRY table in database

CAPITAL table in database

Source code:

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012