In the previous articles, we have learnt about how to write Hibernate Hello World Example using XML Mapping and JPA Annotation. In this Hibernate One To One Mapping Example, we will learn how to do One To One Mapping in Hibernate
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" ( "ID" NUMBER(10,0) NOT NULL ENABLE, "NAME" VARCHAR2(255 CHAR), "AGE" NUMBER(10,0), "DEPT" VARCHAR2(255 CHAR), PRIMARY KEY ("ID") ); CREATE TABLE "EMPLOYEE_ADDRESS" ( "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 (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 Mapping Example
Employee.java
Create a new Java file Employee.java under the package com.javainterviewpoint and add the following code
package com.javainterviewpoint; import java.io.Serializable; public class Employee implements Serializable { private static final long serialVersionUID = -889976693182180703L; private int id; private String name; private int age; private String dept; private Employee_Address employeeAddress; public Employee() { super(); } public Employee(int id, String name, int age, String dept, Employee_Address employeeAddress) { super(); this.id = id; this.name = name; this.age = age; this.dept = dept; this.employeeAddress = employeeAddress; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public Employee_Address getEmployeeAddress() { return employeeAddress; } public void setEmployeeAddress(Employee_Address employeeAddress) { this.employeeAddress = employeeAddress; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + ", 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).
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 java.io.Serializable; public class Employee_Address implements Serializable { private static final long serialVersionUID = 1L; private int Id; private String Street; private String City; private String State; private String Country; private Employee employee; public Employee_Address() { super(); } public Employee_Address(int id, String street, String city, String state, String country, Employee employee) { super(); Id = id; Street = street; City = city; State = state; Country = country; this.employee = employee; } public int getId() { return Id; } public void setId(int id) { Id = id; } public String getStreet() { return Street; } public void setStreet(String street) { Street = street; } public String getCity() { return City; } public void setCity(String city) { City = city; } public String getState() { return State; } public void setState(String state) { State = state; } public String getCountry() { return Country; } public void setCountry(String country) { Country = country; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } @Override public String toString() { return "Employee_Address [Id=" + Id + ", Street=" + Street + ", City=" + City + ", State=" + State + ", Country=" + Country + ", employee=" + employee + "]"; } }
employee.hbm.xml
Place the employee.hbm.xml file under the src/main/resources folder
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.javainterviewpoint"> <class name="Employee" table="EMPLOYEE"> <id name="id" column="ID"> <generator class="native" /> </id> <one-to-one name="EmployeeAddress" class="Employee_Address" cascade="all"></one-to-one> <property name="name" column="NAME" /> <property name="age" column="AGE" /> <property name="dept" column="DEPT" /> </class> </hibernate-mapping>
- The “employee.hbm.xml” tells Hibernate to map “Employee.class” with the “EMPLOYEE” table in the database.
- Next tag is the <id> tag, this tag tells which column needs to be marked as primary key in the database table, here our id property of the Employee class is the primary key. We have selected the generator as native, it takes the sequence in Oracle, if no sequence name is provided then “HIBERNATE_SEQUENCE” will be used
- We have mapped the Employee_Address class with this Employee class with Hibernate one to one mapping.
- The property name, age, dept are mapped with NAME, AGE, DEPT columns in the table respectively.
employee_address.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.javainterviewpoint"> <class name="Employee_Address" table="EMPLOYEE_ADDRESS"> <id name="Id" column="ID"> <generator class="foreign"> <param name="property">Employee</param> </generator> </id> <one-to-one name="Employee" class="Employee" constrained="true"></one-to-one> <property name="Street" column="STREET" /> <property name="City" column="CITY" /> <property name="State" column="STATE" /> <property name="Country" column="COUNTRY" /> </class> </hibernate-mapping>
- The “employee_address.hbm.xml” tells hibernate to map “Employee_Address.class” with the “EMPLOYEE_ADDRESS” table in the database.
- For the <id> tag we have selected the generator as foreign, so that it uses the PRIMARY KEY of the EMPLOYEE table.
- We have mapped the Employee class with this Employee_Address class with Hibernate one to one mapping.With Constrianed=”true” will make sure that EMPLOYEE must exist.
- The property Street, City, State, Country are mapped with STREET, CITY, STATE, COUNTRY columns in the table respectively.
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 resource file --> <mapping resource="employee.hbm.xml" /> <mapping resource="employee_address.hbm.xml" /> </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 resource tag we need to specify all the mapping file for which we need the table to be created or updated.
<mapping resource="employee.hbm.xml" /> <mapping resource="employee_address.hbm.xml" />
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 Employee object Employee employee = new Employee(); //Set value to Employee class properties employee.setAge(102); employee.setDept("IT"); employee.setName("JIP"); //Create new Employee Address Object Employee_Address employeeAddress = new Employee_Address(); //Set values to Employee_Address class properties employeeAddress.setStreet("Test Street"); employeeAddress.setCity("Test City"); employeeAddress.setState("Test State"); employeeAddress.setCountry("Test Countty"); //Bi directional association employee.setEmployeeAddress(employeeAddress); employeeAddress.setEmployee(employee); //Persist 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.setAge(102); employee.setDept("IT"); employee.setName("JIP");
- Create a new Employee_Address object and set value to it properties
Employee_Address employeeAddress = new Employee_Address(); employeeAddress.setStreet("Test Street"); employeeAddress.setCity("Test City"); employeeAddress.setState("Test State"); employeeAddress.setCountry("Test Countty");
- 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:
Nov 01, 2016 3:03:08 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: EMPLOYEE Nov 01, 2016 3:03:08 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, age, name, dept] Nov 01, 2016 3:03:08 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] Nov 01, 2016 3:03:08 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014161] Nov 01, 2016 3:03:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: EMPLOYEE_ADDRESS Nov 01, 2016 3:03:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, street, state, country, city] Nov 01, 2016 3:03:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [fk_n9wnkgtgib3vipu1g7t02572y] Nov 01, 2016 3:03:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014319] Nov 01, 2016 3:03:12 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into EMPLOYEE (NAME, AGE, DEPT, ID) values (?, ?, ?, ?) Hibernate: insert into EMPLOYEE_ADDRESS (STREET, CITY, STATE, COUNTRY, ID) values (?, ?, ?, ?, ?)
RetrieveEmployee.java
package com.javainterviewpoint; import java.util.List; 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(); List employeeList = session.createQuery("from Employee").list(); for(Employee employee : employeeList) { System.out.println("*** Employee Details ***"); System.out.println("Employee ID : "+employee.getId()); System.out.println("Employee Name : "+employee.getName()); System.out.println("Employee Age : "+employee.getAge()); System.out.println("Employee Dept : "+employee.getDept()); System.out.println("*** Employee Address ***"); Employee_Address employeeAddress = (Employee_Address)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(); } //Close the session session.close(); } }
Output:
Hibernate: select employee0_.ID as ID1_0_, employee0_.NAME as NAME2_0_, employee0_.AGE as AGE3_0_, employee0_.DEPT as DEPT4_0_ from EMPLOYEE employee0_ Hibernate: select employee_a0_.ID as ID1_1_0_, employee_a0_.STREET as STREET2_1_0_, employee_a0_.CITY as CITY3_1_0_, employee_a0_.STATE as STATE4_1_0_, employee_a0_.COUNTRY as COUNTRY5_1_0_ from EMPLOYEE_ADDRESS employee_a0_ where employee_a0_.ID=? Hibernate: select employee_a0_.ID as ID1_1_0_, employee_a0_.STREET as STREET2_1_0_, employee_a0_.CITY as CITY3_1_0_, employee_a0_.STATE as STATE4_1_0_, employee_a0_.COUNTRY as COUNTRY5_1_0_ from EMPLOYEE_ADDRESS employee_a0_ where employee_a0_.ID=? *** Employee Details *** Employee ID : 31 Employee Name : JIP Employee Age : 102 Employee Dept : IT *** Employee Address *** Street : Test Street City : Test City State : Test State Country : Test Countty *** Employee Details *** Employee ID : 28 Employee Name : JIP Employee Age : 102 Employee Dept : IT *** Employee Address *** Street : Test Street City : Test City State : Test State Country : Test Countty
Leave a Reply