Hibernate One-to-One Bidirectional Mapping – Primary Key(Annotation)

In this article, we will learn how to achieve Hibernate One-to-One Bidirectional Mapping using the JPA Annotations. This Annotation approach is just an alternative to the XML mapping which we used in our earlier article Hibernate One-to-One Bidirectional XML Mapping Example with Primary Key.

In this approach, we will have two tables sharing the same primary key. In our example, we have two tables EMPLOYEE and EMPLOYEE_ADDRESS sharing the same primary key EMP_ID. This gives us the possibility of traversing EMPLOYEE_ADDRESS from EMPLOYEE and vice-versa and hence called as Bidirectional relationship.

Creating table

Create EMPLOYEE and EMPLOYEE_ADDRESS Tables, simply Copy and Paste the following SQL query in the query editor to get the table created.

CREATE TABLE "EMPLOYEE" 
   (
   "EMP_ID" NUMBER(10,0) NOT NULL ENABLE, 
   "EMP_NAME" VARCHAR2(255 CHAR), 
    PRIMARY KEY (EMP_ID)
   );
 
CREATE TABLE "EMPLOYEE_ADDRESS" 
  ( 
  "EMP_ID" NUMBER(10,0) NOT NULL ENABLE, 
  "STREET" VARCHAR2(255 CHAR), 
  "CITY" VARCHAR2(255 CHAR), 
  "STATE" VARCHAR2(255 CHAR), 
  "COUNTRY" VARCHAR2(255 CHAR), 
   PRIMARY KEY (EMP_ID) 
  );

Folder Structure:

Hibernate One-To-One Bidirectional Mapping

  1. Create a simple Maven Project “HibernateTutorial” and create a package for our source files com.javainterviewpointunder  src/main/java 
  2. Now add the following dependency in the POM.xml
     <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>HibernateTutorial</groupId>
       <artifactId>HibernateTutorial</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <properties>
          <hibernate.version>4.3.11.Final</hibernate.version>
          <oracle.connector.version>11.2.0</oracle.connector.version>
       </properties>
    
       <dependencies>
         <!-- Hibernate -->
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
         </dependency>
    
         <!-- Oracle -->
         <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>${oracle.connector.version}</version>
         </dependency>
       </dependencies>
       <build>
         <sourceDirectory>src</sourceDirectory>
         <plugins>
           <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.3</version>
             <configuration>
             <source>1.7</source>
             <target>1.7</target>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </project>
  3. Create the Java classes Employee.java, Employee_Address.java, EmployeeHibernateOneToOne.java and RetrieveEmployee.java under  com.javainterviewpoint folder.
  4. Place the employee.hbm.xml, employee_address.hbm.xml, hibernate.cfg.xml under the src/main/resources  directory

Hibernate One-To-One Bidirectional Mapping

Employee.java

Create a new Java file Employee.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

import javax.persistence.CascadeType;
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;

@Entity
@Table(name="EMPLOYEE")
public class Employee
{
    @Id
    @GeneratedValue
    @Column(name="EMP_ID")
    private int empId;
    
    @Column(name="EMP_NAME")
    private String empName;
    
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Employee_Address employeeAddress;

    public Employee()
    {
        super();
    }

    public Employee(int empId, String empName, Employee_Address employeeAddress)
    {
        super();
        this.empId = empId;
        this.empName = empName;
        this.employeeAddress = employeeAddress;
    }

    public int getEmpId()
    {
        return empId;
    }

    public void setEmpId(int empId)
    {
        this.empId = empId;
    }

    public String getEmpName()
    {
        return empName;
    }

    public void setEmpName(String empName)
    {
        this.empName = empName;
    }

    public Employee_Address getEmployeeAddress()
    {
        return employeeAddress;
    }

    public void setEmployeeAddress(Employee_Address employeeAddress)
    {
        this.employeeAddress = employeeAddress;
    }

    @Override
    public String toString()
    {
        return "Employee [empId=" + empId + ", empName=" + empName + ", employeeAddress=" + employeeAddress + "]";
    }
}

Our Employee class is a simple POJO class consisting of the getters and setters for the Employee class properties (id, name, age, dept).

In the POJO class, we have used the below JPA Annotations.

  1. @Entity – This annotation will mark our Employee class as an Entity Bean.
  2. @Table@Table annotation will map our class to the corresponding database table. You can also specify other attributes such as indexes, catalog, schema, uniqueConstraints. The @Table annotation is an optional annotation if this annotation is not provided then the class name will be used as the table name.
  3. @Id –  The @Id annotation marks the particular field as the primary key of the Entity.
  4. @GeneratedValue – This annotation is used to specify how the primary key should be generated. Here SEQUENCE Strategy will be used as this the default strategy for Oracle
  5. @OneToOne – This annotation on the employeeAddress property of the Employee class indicates that there exist one to one association between Employee_Address Entity and Employee Entity. We have also used “cascade = CascadeType.ALL” since the Employee_Address Entity cannot exist without Employee Entity. If we used this setting whenever the Employee is updated Employee_Address also will be updated.
  6. @PrimaryKeyJoinColum – This annotation indicates that the primary key of the Employee Entity will act as the foreign key for the Employee_Address Entity.
  7. @Column – This annotation maps the corresponding fields to their respective columns in the database table.

Employee_Address.java

Create a new Java file Employee_Address.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

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

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

@Entity
@Table(name="EMPLOYEE_ADDRESS")
public class Employee_Address
{
    @Id
    @GeneratedValue(generator="fkgen")
    @GenericGenerator(name="fkgen",strategy="foreign",
        [email protected](name="property",value="employee"))
    @Column(name="EMP_ID")
    private int empId;
    
    @Column(name="STREET")
    private String street;
    @Column(name="CITY")
    private String city;
    @Column(name="STATE")
    private String state;
    @Column(name="COUNTRY")
    private String country;
    
    @OneToOne(mappedBy="employeeAddress")
    private Employee employee;

    public Employee_Address()
    {
        super();
    }

    public Employee_Address(int empId, String street, String city, String state, String country, Employee employee)
    {
        super();
        this.empId = empId;
        this.street = street;
        this.city = city;
        this.state = state;
        this.country = country;
        this.employee = employee;
    }

    public int getEmpId()
    {
        return empId;
    }

    public void setEmpId(int empId)
    {
        this.empId = empId;
    }

    public String getStreet()
    {
        return street;
    }

    public void setStreet(String street)
    {
        this.street = street;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public String getCountry()
    {
        return country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }

    public Employee getEmployee()
    {
        return employee;
    }

    public void setEmployee(Employee employee)
    {
        this.employee = employee;
    }

    @Override
    public String toString()
    {
        return "Employee_Address [empId=" + empId + ", street=" + street + ", city=" + city + ", state=" + state
                + ", country=" + country + ", employee=" + employee + "]";
    }
}

In the Employee_Address Entity, we have one new annotation @GenericGenerator – This annotation specifies a foreign key strategy in order to generate values for the EmpId column as a foreign key

hibernate.cfg.xml

Place the hibernate.cfg.xml file also under the src/main/resources folder

 <?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 settings -->
 <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>

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

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

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

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

 <!-- Mapping classes-->
 <mapping class="com.javainterviewpoint.Employee" />
 <mapping class="com.javainterviewpoint.Employee_Address" />
</session-factory> 
</hibernate-configuration>
  • First and foremost property is for specifying the JDBC Driver class, in my case it OracleDriver
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  • Give the connection URL for connecting the database and provide username and password for connecting the above database
<property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
  • Specify the connection pool size, this property limits the number of connections in the Hibernate connection pool.
<property name="connection.pool_size">1</property>
  • Dialect Property makes the Hibernate generate the SQL for the corresponding database which is being used. In this example we are using Oracle database hence Oracle query will be generated. If you are using MySQL database then you need to change the dialect accordingly.
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  • The show_sql property will print the executed sql in the console when set to true.
<property name="show_sql">true</property>
  • If the property “hibernate.hbm2ddl.auto” is set to “create”  This will drop and recreate the database schema on every execution. If it is set to “update” then the database schema will be updated every time rather than dropping and recreating.
<property name="hibernate.hbm2ddl.auto">update</property>
  • Under the Mapping class tag  we need to specify all the entity class for which we need the table to be created or updated.
 <mapping class="com.javainterviewpoint.Employee" />
 <mapping class="com.javainterviewpoint.Employee_Address" />

EmployeeHibernateOneToOne.java

package com.javainterviewpoint;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class EmployeeHibernateOneToOne
{
    public static void main(String args[])
    {
        // Reading the hibernate configuration file
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder();
        regBuilber.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = regBuilber.build();

        // Create SessionFacctory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        // Create Session from SessionFactory
        Session session = sessionFactory.openSession();

        // Begin the transaction
        session.beginTransaction();

        // Create a Employee object
        Employee employee = new Employee();
        employee.setEmpName("Employee 111");
        
        Employee_Address employeeAddress = new Employee_Address();
        employeeAddress.setStreet("Street 111");
        employeeAddress.setCity("City 111");
        employeeAddress.setCountry("Country 111");
        employeeAddress.setState("State 111");

        //Setting Bi directional association
        employee.setEmployeeAddress(employeeAddress);
        employeeAddress.setEmployee(employee);
        
        //Save the Employee object
        session.save(employee);
        //Commit the changes
        session.getTransaction().commit();
        //Close the session
        session.close();
    }
}
  • Create the Configuration object and read the configuration file using the configure() method.
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
  • Get the SessionFactory object through the buildSessionFactory() method of the configuration object.
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  • openSession() method opens up the new session and begin a new transaction
Session session = sessionFactory.openSession();
session.beginTransaction();
  • Create a new Employee object and set values to its properties
Employee employee = new Employee();
employee.setEmpName("Employee 111");
  • Create a new Employee_Address object and set value to it properties
    Employee_Address employeeAddress = new Employee_Address();
    employeeAddress.setStreet("Street 111");
    employeeAddress.setCity("City 111");
    employeeAddress.setCountry("Country 111");
    employeeAddress.setState("State 111");
  • save() method of the session object will persist the employee object into the database.
session.save(employee);
  • Finally get the transaction and commit the changes and close the session.
session.getTransaction().commit();
session.close();

Console:

INFO: HHH000261: Table found: EMPLOYEE
Dec 08, 2016 6:10:52 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [emp_name, emp_id]
Dec 08, 2016 6:10:52 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Dec 08, 2016 6:10:52 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0014543]
Dec 08, 2016 6:10:55 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: EMPLOYEE_ADDRESS
Dec 08, 2016 6:10:55 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [street, emp_id, state, country, city]
Dec 08, 2016 6:10:55 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Dec 08, 2016 6:10:55 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0014722]
Dec 08, 2016 6:10:55 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into EMPLOYEE (EMP_NAME, EMP_ID) values (?, ?)
Hibernate: insert into EMPLOYEE_ADDRESS (CITY, COUNTRY, STATE, STREET, EMP_ID) values (?, ?, ?, ?, ?)

RetrieveEmployee.java

package com.javainterviewpoint;

import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class RetrieveEmployee
{
    public static void main(String args[])
    {
        //Reading the hibernate configuration file
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder();
        regBuilber.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = regBuilber.build();
        
        //Create SessionFacctory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        // Retrieving Employee and Department
        System.out.println("** Employee Address through Employee **");
        List empList = session.createQuery("from Employee").list();
        for(Employee employee : empList)
        {
            System.out.println("** Employee Details **");
            System.out.println("Employee Id   : "+ employee.getEmpId());
            System.out.println("Employee Name : "+  employee.getEmpName());
            
            //Retrieving 
            System.out.println("** Employee Address Details **");
            Employee_Address employeeAddress = employee.getEmployeeAddress();
            System.out.println("Street  : " + employeeAddress.getStreet());
            System.out.println("City    : " + employeeAddress.getCity());
            System.out.println("State   : " + employeeAddress.getState());
            System.out.println("Country : " + employeeAddress.getCountry());
        }
        
        System.out.println("*** Retrieving Employee through Employee Address *** ");
        List addrList = session.createQuery("from Employee_Address").list();
        for(Employee_Address employeeAddress : addrList)
        {   
            System.out.println("** Employee Details **");
            Employee employee = employeeAddress.getEmployee();
            System.out.println("Employee Id   : "+ employee.getEmpId());
            System.out.println("Employee Name : "+  employee.getEmpName());
            
            //Retrieving 
            System.out.println("** Employee Address Details **");
            System.out.println("Street  : " + employeeAddress.getStreet());
            System.out.println("City    : " + employeeAddress.getCity());
            System.out.println("State   : " + employeeAddress.getState());
            System.out.println("Country : " + employeeAddress.getCountry());
        }
        //Close the session
        session.close();
    }
}

Output:

** Employee Address through Employee **
Hibernate: select employee0_.EMP_ID as EMP_ID1_0_, employee0_.EMP_NAME as EMP_NAME2_0_ from EMPLOYEE employee0_
Hibernate: select employee_a0_.EMP_ID as EMP_ID1_1_0_, employee_a0_.CITY as CITY2_1_0_, employee_a0_.COUNTRY as COUNTRY3_1_0_, employee_a0_.STATE as STATE4_1_0_, employee_a0_.STREET as STREET5_1_0_, employee1_.EMP_ID as EMP_ID1_0_1_, employee1_.EMP_NAME as EMP_NAME2_0_1_ from EMPLOYEE_ADDRESS employee_a0_ left outer join EMPLOYEE employee1_ on employee_a0_.EMP_ID=employee1_.EMP_ID where employee_a0_.EMP_ID=?
** Employee Details **
Employee Id   : 191
Employee Name : Employee 111
** Employee Address Details **
Street  : Street 111
City    : City 111
State   : State 111
Country : Country 111
*** Retrieving Employee through Employee Address *** 
Hibernate: select employee_a0_.EMP_ID as EMP_ID1_1_, employee_a0_.CITY as CITY2_1_, employee_a0_.COUNTRY as COUNTRY3_1_, employee_a0_.STATE as STATE4_1_, employee_a0_.STREET as STREET5_1_ from EMPLOYEE_ADDRESS employee_a0_
** Employee Details **
Employee Id   : 191
Employee Name : Employee 111
** Employee Address Details **
Street  : Street 111
City    : City 111
State   : State 111
Country : Country 111

Leave a Reply

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