Javatpoint Logo

91-9990449935

 0120-4256464

One to One Mapping in Hibernate by one-to-one example

As We have discussed in the previous example, there are two ways to perform one to one mapping in hibernate:

  • By many-to-one element
  • By one-to-one element

Here, we are going to perform one to one mapping by one-to-one element. In such case, no foreign key is created in the primary table.

In this example, one employee can have one address and one address belongs to one employee only. Here, we are using bidirectional association. Let's look at the persistent classes.

1) Persistent classes for one to one mapping

There are two persistent classes Employee.java and Address.java. Employee class contains Address class reference and vice versa.

Employee.java

  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int employeeId;  
  5. private String name,email;  
  6. private Address address;  
  7. //setters and getters  
  8. }  

Address.java

  1. package com.javatpoint;  
  2.   
  3. public class Address {  
  4. private int addressId;  
  5. private String addressLine1,city,state,country;  
  6. private int pincode;  
  7. private Employee employee;  
  8. //setters and getters  
  9. }  

2) Mapping files for the persistent classes

The two mapping files are employee.hbm.xml and address.hbm.xml.

employee.hbm.xml

In this mapping file we are using one-to-one element in both the mapping files to make the one to one mapping.

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6.            <hibernate-mapping>  
  7.           <class name="com.javatpoint.Employee" table="emp212">  
  8.           <id name="employeeId">  
  9.           <generator class="increment"></generator>  
  10.           </id>  
  11.           <property name="name"></property>  
  12.           <property name="email"></property>  
  13.             
  14.           <one-to-one name="address" cascade="all"></one-to-one>  
  15.           </class>  
  16.             
  17.           </hibernate-mapping>  

address.hbm.xml

This is the simple mapping file for the Address class. But the important thing is generator class. Here, we are using foreign generator class that depends on the Employee class primary key.

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6.            <hibernate-mapping>  
  7.           <class name="com.javatpoint.Address" table="address212">  
  8.           <id name="addressId">  
  9.           <generator class="foreign">  
  10.           <param name="property">employee</param>  
  11.           </generator>  
  12.           </id>  
  13.           <property name="addressLine1"></property>  
  14.           <property name="city"></property>  
  15.           <property name="state"></property>  
  16.           <property name="country"></property>  
  17.             
  18.           <one-to-one name="employee"></one-to-one>  
  19.           </class>  
  20.             
  21.           </hibernate-mapping>  

3) Configuration file

This file contains information about the database and mapping file.

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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="hbm2ddl.auto">update</property>  
  11.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  12.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  13.         <property name="connection.username">system</property>  
  14.         <property name="connection.password">oracle</property>  
  15.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  16.     <mapping resource="employee.hbm.xml"/>  
  17.     <mapping resource="address.hbm.xml"/>  
  18.     </session-factory>  
  19.   
  20. </hibernate-configuration>  

4) User classes to store and fetch the data

Store.java

  1. package com.javatpoint;  
  2. import org.hibernate.cfg.*;  
  3. import org.hibernate.*;  
  4.   
  5. public class Store {  
  6. public static void main(String[] args) {  
  7.     Configuration cfg=new Configuration();  
  8.     cfg.configure("hibernate.cfg.xml");  
  9.     SessionFactory sf=cfg.buildSessionFactory();  
  10.     Session session=sf.openSession();  
  11.     Transaction tx=session.beginTransaction();  
  12.       
  13.     Employee e1=new Employee();  
  14.     e1.setName("Ravi Malik");  
  15.     e1.setEmail("ravi@gmail.com");  
  16.       
  17.     Address address1=new Address();  
  18.     address1.setAddressLine1("G-21,Lohia nagar");  
  19.     address1.setCity("Ghaziabad");  
  20.     address1.setState("UP");  
  21.     address1.setCountry("India");  
  22.     address1.setPincode(201301);  
  23.       
  24.       
  25.     e1.setAddress(address1);  
  26.     address1.setEmployee(e1);  
  27.       
  28.     session.persist(e1);  
  29.     tx.commit();  
  30.       
  31.     session.close();  
  32.     System.out.println("success");  
  33. }  
  34. }  

Fetch.java

  1. package com.javatpoint;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4. import org.hibernate.Query;  
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.cfg.Configuration;  
  8.   
  9. public class Fetch {  
  10. public static void main(String[] args) {  
  11.     Configuration cfg=new Configuration();  
  12.     cfg.configure("hibernate.cfg.xml");  
  13.     SessionFactory sf=cfg.buildSessionFactory();  
  14.     Session session=sf.openSession();  
  15.       
  16.     Query query=session.createQuery("from Employee e");  
  17.     List<Employee> list=query.list();  
  18.       
  19.     Iterator<Employee> itr=list.iterator();  
  20.     while(itr.hasNext()){  
  21.      Employee emp=itr.next();  
  22.      System.out.println(emp.getEmployeeId()+" "+emp.getName()+" "+emp.getEmail());  
  23.      Address address=emp.getAddress();  
  24.      System.out.println(address.getAddressLine1()+" "+address.getCity()+" "+  
  25.         address.getState()+" "+address.getCountry());  
  26.     }  
  27.   
  28.     session.close();  
  29.     System.out.println("success");  
  30. }  
  31. }