One to One Annotation Mapping in Hibernate

Let us understand about One to One Annotation Mapping in Hibernate


In this relation mapping, one object of a class is associated with only one object of another class.
In other words,
One record of a table is associated with only one record of another table.

In this mapping, both the tables will share the common primary key.

Example:

Consider the relation between Applicant and Passport

One Applicant will have only one Passport.

Let’s develop One To One relation between Applicant and Passport

Relation looks as below

OneToOne_ERD

As shown in the above ER diagram, Relation between Applicant and Passport is One To One.

Passport_Id column is the primary key of Passport table

Applicant_Id column is the primary key of Applicant table

Note:
Both Applicant and Passport tables will share the same value for primary key. hence Applicant_Id and Passport_Id will have the same value for each record.

Step 1

Create hibernate project

Please refer Hibernate setup in eclipse article on how to do it.

Project structure

OneToOneAnnotationProjStructure

Step 2

Update pom.xml with Hibernate and Mysql dependencies

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>OneToOneAnnotation</groupId>
  6.   <artifactId>OneToOneAnnotation</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>OneToOneAnnotation</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.  
  19.     <dependency>
  20.       <groupId>junit</groupId>
  21.       <artifactId>junit</artifactId>
  22.       <version>3.8.1</version>
  23.       <scope>test</scope>
  24.     </dependency>
  25.      <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  26.         <dependency>
  27.             <groupId>org.hibernate</groupId>
  28.             <artifactId>hibernate-core</artifactId>
  29.             <version>5.2.6.Final</version>
  30.         </dependency>
  31. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
  32.         <dependency>
  33.             <groupId>org.hibernate</groupId>
  34.             <artifactId>hibernate-annotations</artifactId>
  35.             <version>3.5.6-Final</version>
  36.         </dependency>
  37.  
  38.         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  39.         <dependency>
  40.             <groupId>mysql</groupId>
  41.             <artifactId>mysql-connector-java</artifactId>
  42.             <version>6.0.5</version>
  43.         </dependency>
  44.   </dependencies>
  45. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>OneToOneAnnotation</groupId>
  <artifactId>OneToOneAnnotation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>OneToOneAnnotation</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.2.6.Final</version>
		</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.5.6-Final</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.5</version>
		</dependency>
  </dependencies>
</project>

Step 3

Create Applicant class

  1. package com.kb.model;
  2.  
  3. import javax.persistence.CascadeType;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToOne;
  10. import javax.persistence.Table;
  11.  
  12. @Entity
  13. @Table(name="Applicant")
  14. public class Applicant {
  15.    
  16.     @Id
  17.     @GeneratedValue(strategy = GenerationType.SEQUENCE)
  18.     @Column(name = "Applicant_Id")
  19.     private int applicantId;
  20.    
  21.     @Column(name = "name")
  22.     private String name;
  23.    
  24.     @Column(name="Age")
  25.     private int age;
  26.    
  27.     @Column(name="city")
  28.     private String city;
  29.    
  30.     @OneToOne(mappedBy="applicant", cascade=CascadeType.ALL)
  31.     private Passport passport;
  32.    
  33.     public int getApplicantId() {
  34.         return applicantId;
  35.     }
  36.     public void setApplicantId(int applicantId) {
  37.         this.applicantId = applicantId;
  38.     }
  39.    
  40.     public String getName() {
  41.         return name;
  42.     }
  43.     public void setName(String name) {
  44.         this.name = name;
  45.     }
  46.     public String getCity() {
  47.         return city;
  48.     }
  49.     public void setCity(String city) {
  50.         this.city = city;
  51.     }
  52.     public int getAge() {
  53.         return age;
  54.     }
  55.     public void setAge(int age) {
  56.         this.age = age;
  57.     }
  58.     public Passport getPassport() {
  59.         return passport;
  60.     }
  61.     public void setPassport(Passport passport) {
  62.         this.passport = passport;
  63.     }
  64.    
  65. }
package com.kb.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Applicant")
public class Applicant {
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	@Column(name = "Applicant_Id")
	private int applicantId;
	
	@Column(name = "name")
	private String name;
	
	@Column(name="Age")
	private int age;
	
	@Column(name="city")
	private String city;
	
	@OneToOne(mappedBy="applicant", cascade=CascadeType.ALL)
	private Passport passport;
	
	public int getApplicantId() {
		return applicantId;
	}
	public void setApplicantId(int applicantId) {
		this.applicantId = applicantId;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Passport getPassport() {
		return passport;
	}
	public void setPassport(Passport passport) {
		this.passport = passport;
	}
	
}


We have specified Primary key as applicantId and generator class as “sequence” for automatic primary key generation.

We have defined one to one mapping with Passport entity using OneToOne annotation

We have also provided cascade attribute value as “All” which means whenever we any operations like save or update on Applicant record it will also perform the same operation on Passport record automatically.

Step 4

Create Passport class

  1. package com.kb.model;
  2.  
  3. import java.util.Date;
  4.  
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToOne;
  10. import javax.persistence.PrimaryKeyJoinColumn;
  11. import javax.persistence.Table;
  12.  
  13. import org.hibernate.annotations.GenericGenerator;
  14. import org.hibernate.annotations.Parameter;
  15.  
  16. @Entity
  17. @Table(name="Passport")
  18. public class Passport {
  19.     @Id
  20.     @GeneratedValue(generator="gen")
  21.     @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property",  
  22.                           value="applicant"))
  23.     @Column(name = "Passport_Id")
  24.     private int passportId;
  25.    
  26.     @Column(name = "Passport_Number")
  27.     private String passportNumber;
  28.    
  29.     @Column(name = "Issued_Date")
  30.     private Date issuedDate;
  31.    
  32.     @Column(name = "Expiry_Date")
  33.     private Date expiryDate;
  34.    
  35.     @OneToOne
  36.     @PrimaryKeyJoinColumn
  37.     private Applicant applicant;
  38.    
  39.     public int getPassportId() {
  40.         return passportId;
  41.     }
  42.     public void setPassportId(int passportId) {
  43.         this.passportId = passportId;
  44.     }
  45.     public String getPassportNumber() {
  46.         return passportNumber;
  47.     }
  48.     public void setPassportNumber(String passportNumber) {
  49.         this.passportNumber = passportNumber;
  50.     }
  51.     public Date getIssuedDate() {
  52.         return issuedDate;
  53.     }
  54.     public void setIssuedDate(Date issuedDate) {
  55.         this.issuedDate = issuedDate;
  56.     }
  57.     public Date getExpiryDate() {
  58.         return expiryDate;
  59.     }
  60.     public void setExpiryDate(Date expiryDate) {
  61.         this.expiryDate = expiryDate;
  62.     }
  63.     public Applicant getApplicant() {
  64.         return applicant;
  65.     }
  66.     public void setApplicant(Applicant applicant) {
  67.         this.applicant = applicant;
  68.     }
  69. }
package com.kb.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="Passport")
public class Passport {
	@Id
	@GeneratedValue(generator="gen")
	@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property",  
                          value="applicant"))
	@Column(name = "Passport_Id")
	private int passportId;
	
	@Column(name = "Passport_Number")
	private String passportNumber;
	
	@Column(name = "Issued_Date")
	private Date issuedDate;
	
	@Column(name = "Expiry_Date")
	private Date expiryDate;
	
	@OneToOne
	@PrimaryKeyJoinColumn
	private Applicant applicant;
	
	public int getPassportId() {
		return passportId;
	}
	public void setPassportId(int passportId) {
		this.passportId = passportId;
	}
	public String getPassportNumber() {
		return passportNumber;
	}
	public void setPassportNumber(String passportNumber) {
		this.passportNumber = passportNumber;
	}
	public Date getIssuedDate() {
		return issuedDate;
	}
	public void setIssuedDate(Date issuedDate) {
		this.issuedDate = issuedDate;
	}
	public Date getExpiryDate() {
		return expiryDate;
	}
	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}
	public Applicant getApplicant() {
		return applicant;
	}
	public void setApplicant(Applicant applicant) {
		this.applicant = applicant;
	}
}


We have specified Primary key as “passportId” and generator strategy as “foreign”.

We have defined one to one mapping with Applicant entity using “one-to-one” tag.

@PrimaryKeyJoinColumn – Specifies a primary key column that is used as a foreign key to join to another table.

In our case, it specifies Primary key of Applicant table to be used as join column to establish the mapping.

Step 5

Create hibernate.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8.    <session-factory>
  9.  
  10.        <!-- Database connection properties -->
  11.        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  12.        <property name="connection.url">jdbc:mysql://localhost/javainsimpleway</property>
  13.        <property name="connection.username">root</property>
  14.        <property name="connection.password">root</property>
  15.  
  16.        <!-- JDBC connection pool (using the built-in) -->
  17.        <property name="connection.pool_size">10</property>
  18.  
  19.        <!-- SQL dialect -->
  20.        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  21.  
  22.        <!-- Disable the second-level cache -->
  23.        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  24.  
  25.        <!-- Echo all executed SQL to stdout -->
  26.        <property name="show_sql">true</property>
  27.        
  28.        <!-- Format the generated Sql -->
  29.        <property name="format_sql">true</property>
  30.  
  31.        <!-- Dont Drop and re-create the database schema on startup,Just update it -->
  32.        <property name="hbm2ddl.auto">update</property>
  33.  
  34.       <mapping class="com.kb.model.Applicant" />
  35.       <mapping class="com.kb.model.Passport" />
  36.    </session-factory>
  37.  
  38. </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
       "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
   <session-factory>
 
       <!-- Database connection properties -->
       <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
       <property name="connection.url">jdbc:mysql://localhost/javainsimpleway</property>
       <property name="connection.username">root</property>
       <property name="connection.password">root</property>
 
       <!-- JDBC connection pool (using the built-in) -->
       <property name="connection.pool_size">10</property>
 
       <!-- SQL dialect -->
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
       <!-- Disable the second-level cache -->
       <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
 
       <!-- Echo all executed SQL to stdout -->
       <property name="show_sql">true</property>
       
       <!-- Format the generated Sql -->
       <property name="format_sql">true</property>
 
       <!-- Dont Drop and re-create the database schema on startup,Just update it -->
       <property name="hbm2ddl.auto">update</property>
 
      <mapping class="com.kb.model.Applicant" />
	  <mapping class="com.kb.model.Passport" /> 
   </session-factory>
 
</hibernate-configuration>


We have defined all the database configuration in this file

hbm2ddl.auto property is defined in the config file which helps in automatic creation of tables in the database based on the mapping.

We have also provided the mapping xml file location using “mapping” tag.

Step 6

Create Hibernate util class

  1. package com.kb.util;
  2.  
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5.  
  6. public class HibernateUtil {
  7.     private static final SessionFactory sessionFactory = buildSessionFactory();
  8.  
  9.     private static SessionFactory buildSessionFactory() {
  10.         try {
  11.             // Create the SessionFactory from hibernate.cfg.xml
  12.             return new Configuration().configure().buildSessionFactory();
  13.         } catch (Throwable ex) {
  14.             // Make sure you log the exception to track it
  15.             System.err.println("SessionFactory creation failed." + ex);
  16.             throw new ExceptionInInitializerError(ex);
  17.         }
  18.     }
  19.  
  20.     public static SessionFactory getSessionFactory() {
  21.         return sessionFactory;
  22.     }
  23.    
  24.     public static void shutdown() {
  25.         // Optional but can be used to Close caches and connection pools
  26.         getSessionFactory().close();
  27.     }
  28.  
  29. }
package com.kb.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception to track it
            System.err.println("SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    	// Optional but can be used to Close caches and connection pools
    	getSessionFactory().close();
    }

}

Step 7

Create main class to interact with DB

  1. package com.kb.db;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5.  
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8. import org.hibernate.Transaction;
  9.  
  10. import com.kb.model.Applicant;
  11. import com.kb.model.Passport;
  12. import com.kb.util.HibernateUtil;
  13.  
  14. public class Main {
  15.     public static void main(String[] args) {
  16.         // Get session factory using Hibernate Util class
  17.         SessionFactory sf = HibernateUtil.getSessionFactory();
  18.         // Get session from Sesson factory
  19.         Session session = sf.openSession();
  20.  
  21.         // Begin transaction
  22.         Transaction t = session.beginTransaction();
  23.        
  24.         //Create Applicant Model data
  25.         Applicant applicant = new Applicant();
  26.         applicant.setName("John");
  27.         applicant.setAge(28);
  28.         applicant.setCity("Newyork");
  29.  
  30.         //Create Passport Model data
  31.         Passport passport = new Passport();
  32.         passport.setPassportNumber("ABCDE1111Z");
  33.         passport.setIssuedDate(new Date());
  34.         Calendar date = Calendar.getInstance();
  35.         date.setTime(new Date());
  36.         date.add(Calendar.YEAR, 10);
  37.         passport.setExpiryDate(date.getTime());
  38.  
  39.         applicant.setPassport(passport);
  40.         passport.setApplicant(applicant);
  41.         session.persist(applicant);
  42.  
  43.         // Commit the transaction and close the session
  44.         t.commit();
  45.         session.close();
  46.         System.out.println("successfully persisted Applicant details");
  47.     }
  48.  
  49. }
package com.kb.db;

import java.util.Calendar;
import java.util.Date;

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

import com.kb.model.Applicant;
import com.kb.model.Passport;
import com.kb.util.HibernateUtil;

public class Main {
	public static void main(String[] args) {
		// Get session factory using Hibernate Util class
		SessionFactory sf = HibernateUtil.getSessionFactory();
		// Get session from Sesson factory
		Session session = sf.openSession();

		// Begin transaction
		Transaction t = session.beginTransaction();
		
		//Create Applicant Model data
		Applicant applicant = new Applicant();
		applicant.setName("John");
		applicant.setAge(28);
		applicant.setCity("Newyork");

		//Create Passport Model data
		Passport passport = new Passport();
		passport.setPassportNumber("ABCDE1111Z");
		passport.setIssuedDate(new Date());
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		date.add(Calendar.YEAR, 10);
		passport.setExpiryDate(date.getTime());

		applicant.setPassport(passport);
		passport.setApplicant(applicant);
		session.persist(applicant);

		// Commit the transaction and close the session
		t.commit();
		session.close();
		System.out.println("successfully persisted Applicant details");
	}

}


We have created one Applicant and associated a Passport to that Applicant.

We are persisting only Applicant but both Applicant and Passport will be saved into DB.
It is because we have given cascade=All in Applicant class.

Step 8

Run the above class to check the output

Hibernate: 
    
    create table Applicant (

       Applicant_Id integer not null,

        Name varchar(255),

        Age integer,

        City varchar(255),

        primary key (Applicant_Id)
    )


Hibernate: 
    
    create table Passport (

       Passport_Id integer not null,

        Passport_Number varchar(255),

        Issued_Date date,

        Expiry_Date date,

        primary key (Passport_Id)
    )


Hibernate: 
    
    alter table Passport 

       add constraint FKh6nj623boddru6w09ftabw82g 

       foreign key (Passport_Id) 

       references Applicant (Applicant_Id)


Hibernate: 

    select
        max(Applicant_Id) 

    from
        Applicant


Hibernate: 

    insert 

    into
        Applicant
        (Name, Age, City, Applicant_Id) 

    values
        (?, ?, ?, ?)


Hibernate: 

    insert 

    into
        Passport
        (Passport_Number, Issued_Date, Expiry_Date, Passport_Id) 

    values
        (?, ?, ?, ?)

successfully persisted Applicant details


We can see that Create statement is executed for creating both Applicant and Passport tables.

We can see that Foreign key constraint is created on Passport table referencing the primary key of Applicant table..

2 insert statements are executed (one for Applicant and one for Passport).

Check Table in MYSQL console

E:\MySql_Install\bin
Mysql –u root –p
Enter password

Use javainsimpleway;

Select * from Applicant;

Select * from Passport;

OneToOneAnnotationOutput

We can see that same primary key is shared between Applicant and Passport table.

Download this project OneToOneAnnotation.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on