Hibernate One to One Example using XML

There are two different ways to perform one to one mapping in hibernate:

  • By many-to-one component (utilizing unique="true" attribute)
  • By one-to-one element

Here, we will perform one to one mapping by coordinated component. In such case, no outside key is made in the essential table.

In this model, one representative can have one location and one location has a place with one worker as it were. Here, we are utilizing bidirectional affiliation. We should take a gander at the steady classes.

1) Persistent classes for one to one mapping

There are two tenacious classes Employee.java and Address.java. Worker class contains Address class reference and bad habit versa.

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

Employee.java

  1. package com.javacodegeeks;  
  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.javacodegeeks;  
  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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.            <hibernate-mapping>  
  7.           <class name="com.javacodegeeks.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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.            <hibernate-mapping>  
  7.           <class name="com.javacodegeeks.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.           <property name="pincode"></property>  
  18.             
  19.           <one-to-one name="employee"></one-to-one>  
  20.           </class>  
  21.             
  22.           </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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.         <property name="hbm2ddl.auto">update</property>  
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  12.         <property name="connection.username">system</property>  
  13.         <property name="connection.password">jtp</property>  
  14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  15.     <mapping resource="employee.hbm.xml"/>  
  16.     <mapping resource="address.hbm.xml"/>  
  17.     </session-factory>  
  18.   
  19. </hibernate-configuration>  

4) User classes to store and fetch the data

Store.java

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





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition