In this article, we will learn how to achieve Hibernate One To One Bidirectional Mapping using the JPA Annotations with Foreign Key, in the previous One To One Bidirectional Mapping Example we used only the Primary Key. This Annotation approach is just an alternative to the XML mapping which we used in our earlier article Hibernate One To One Mapping XML Example with Foreign Key
In this approach, we will have two tables with different primary keys. The primary key of EMPLOYEE table EMP_ID will act as a foreign key for the EMPLOYEE_ADDRESS table and EMPLOYEE_ADDRESS table will have its own primary key ADDR_ID.
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, "NAME" VARCHAR2(255 CHAR), PRIMARY KEY ("EMP_ID") ); CREATE TABLE "EMPLOYEE_ADDRESS" ( "ADDR_ID" NUMBER(10,0) NOT NULL ENABLE, "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 ("ADDR_ID"), CONSTRAINT fk_emp FOREIGN KEY ("EMP_ID") REFERENCES EMPLOYEE ("EMP_ID") );
Folder Structure:
- Create a simple Maven Project “HibernateTutorial” and create a package for our source files “com.javainterviewpoint” under src/main/java
- 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>
- Create the Java classes Employee.java, Employee_Address.java, EmployeeHibernateOneToOne.java and RetrieveEmployee.java under com.javainterviewpoint folder.
- Place the employee.hbm.xml, employee_address.hbm.xml, hibernate.cfg.xml under the src/main/resources directory
Hibernate One To One Bidirectional Mapping – Foreign Key
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.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="EMPLOYEE") public class Employee { @Id @GeneratedValue @Column(name="EMP_ID") private int empId; @Column(name="NAME") private String empName; @OneToOne(mappedBy="employee") 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.
- @Entity – This annotation will mark our Employee class as an Entity Bean.
- @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.
- @Id – The @Id annotation marks the particular field as the primary key of the Entity.
- @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
- @OneToOne – This annotation on the employeeAddress property of the Employee class indicates that there exist one to one association between Employee_Address Entity. We have also used the mappedBy attribute as “employee” this indicates that this side is not the owner of the relationship.
- @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.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="EMPLOYEE_ADDRESS") public class Employee_Address { @Id @Column(name = "ADDR_ID") @GeneratedValue private int addrId; @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(cascade= CascadeType.ALL) @JoinColumn(name = "EMP_ID") private Employee employee; public Employee_Address() { super(); } public Employee_Address(int addrId, String street, String city, String state, String country, Employee employee) { super(); this.addrId = addrId; this.street = street; this.city = city; this.state = state; this.country = country; this.employee = employee; } public int getAddrId() { return addrId; } public void setAddrId(int addrId) { this.addrId = addrId; } 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 [addrId=" + addrId + ", street=" + street + ", city=" + city + ", state=" + state + ", country=" + country + ", employee=" + employee + "]"; } }
@JoinColumn annotation indicates that this entity will act as the owner of the relationship (This table has a column with a foreign key to the referenced table)
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 22"); // Create a Employee_Address object Employee_Address employeeAddress = new Employee_Address(); employeeAddress.setStreet("Street 22"); employeeAddress.setCity("City 22"); employeeAddress.setState("State 22"); employeeAddress.setCountry("Country 22"); employee.setEmployeeAddress(employeeAddress); employeeAddress.setEmployee(employee); //Save the Employee_Address object session.save(employeeAddress); //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 22");
- Create a new Employee_Address object and set the value to its properties
Employee_Address employeeAddress = new Employee_Address(); employeeAddress.setStreet("Street 22"); employeeAddress.setCity("City 22"); employeeAddress.setCountry("Country 22"); employeeAddress.setState("State 22");
- save() method of the session object will persist the employeeAddress object into the database and also the employee object since we have used CascadeType.ALL
session.save(employeeAddress);
- Finally get the transaction and commit the changes and close the session.
session.getTransaction().commit(); session.close();
Console:
INFO: HHH000261: Table found: EMPLOYEE Dec 14, 2016 5:25:17 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [emp_name, name, emp_id] Dec 14, 2016 5:25:17 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] Dec 14, 2016 5:25:17 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014768] Dec 14, 2016 5:25:20 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: EMPLOYEE_ADDRESS Dec 14, 2016 5:25:20 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [street, emp_id, state, addr_id, country, city] Dec 14, 2016 5:25:20 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [fk_emp] Dec 14, 2016 5:25:20 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014770] Dec 14, 2016 5:25:20 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into EMPLOYEE (NAME, EMP_ID) values (?, ?) Hibernate: insert into EMPLOYEE_ADDRESS (CITY, COUNTRY, EMP_ID, STATE, STREET, ADDR_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("Address ID : " + employeeAddress.getAddrId()); 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("Address ID : " + employeeAddress.getAddrId()); 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_.NAME as NAME2_0_ from EMPLOYEE employee0_ Hibernate: select employee_a0_.ADDR_ID as ADDR_ID1_1_1_, employee_a0_.CITY as CITY2_1_1_, employee_a0_.COUNTRY as COUNTRY3_1_1_, employee_a0_.EMP_ID as EMP_ID6_1_1_, employee_a0_.STATE as STATE4_1_1_, employee_a0_.STREET as STREET5_1_1_, employee1_.EMP_ID as EMP_ID1_0_0_, employee1_.NAME as NAME2_0_0_ 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 : 257 Employee Name : Employee 22 ** Employee Address Details ** Address ID : 256 Street : Street 22 City : City 22 State : State 22 Country : Country 22 *** Retrieving Employee through Employee Address *** Hibernate: select employee_a0_.ADDR_ID as ADDR_ID1_1_, employee_a0_.CITY as CITY2_1_, employee_a0_.COUNTRY as COUNTRY3_1_, employee_a0_.EMP_ID as EMP_ID6_1_, employee_a0_.STATE as STATE4_1_, employee_a0_.STREET as STREET5_1_ from EMPLOYEE_ADDRESS employee_a0_ ** Employee Details ** Employee Id : 257 Employee Name : Employee 22 ** Employee Address Details ** Address ID : 256 Street : Street 22 City : City 22 State : State 22 Country : Country 22
Leave a Reply