In this Hibernate Many To Many Mapping Example, we will learn how Hibernate Many To Many relationship works(Using Annotation). Let’s take the Example of Employee and Department, one Employee can be part of many Departments and similarly, one Department can have many Employees. Let’s dig into the code.
In Many-to-Many relationship a mediator table is mandatory, this table stores the primary key of both tables (EMPLOYEE and DEPARTMENT) as a foreign key.
Creating table
Create EMPLOYEE, DEPARTMENT and EMPLOYEE_DEPARTMENT 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 "DEPARTMENT" ( "DEP_ID" NUMBER(10,0) NOT NULL ENABLE, "DEP_NAME" VARCHAR2(255 CHAR), PRIMARY KEY ("DEP_ID") ); CREATE TABLE "EMPLOYEE_DEPARTMENT" ( "EMP_ID" NUMBER(10,0) NOT NULL ENABLE, "DEP_ID" NUMBER(10,0) NOT NULL ENABLE, PRIMARY KEY (EMP_ID , DEP_ID), CONSTRAINT FK_EMP_ID FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE (EMP_ID), CONSTRAINT FK_DEP_ID FOREIGN KEY (DEP_ID) REFERENCES DEPARTMENT (DEP_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, Department.java, HibernateManyToMany.java and RetrieveData.java under com.javainterviewpoint folder.
- Place the employee.hbm.xml, department.hbm.xml, hibernate.cfg.xml under the src/main/resources directory
Hibernate Many To Many 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.util.Set; 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.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="EMPLOYEE") public class Employee { @Id @Column(name="EMP_ID") @GeneratedValue private int empId; @Column(name="EMP_NAME") private String empName; @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name="EMPLOYEE_DEPARTMENT", joinColumns={@JoinColumn(name="EMP_ID")}, inverseJoinColumns={@JoinColumn(name="DEP_ID")}) private Set<Department> department; public Employee() { super(); } public Employee(String empName) { super(); this.empName = empName; } 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 Set<Department> getDepartment() { return department; } public void setDepartment(Set<Department> department) { this.department = department; } @Override public String toString() { return "Employee [empId=" + empId + ", empName=" + empName + "]"; } }
Our Employee class is a simple POJO class consisting of the getters and setters for the Employee properties (empId, empName, department).
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
- @ManyToMany – This annotation specifies that there exist a many to many relationship between Employee and Department.
- @JoinTable – This annotation is used to define the Linking Table(Employee_Department). It should be be defined by the relationship owner here our Employee Class is the relationship owner.
- @JoinColumn – This annotation defines the joining column in both the tables.
- @Column – This annotation maps the corresponding fields to their respective columns in the database table.
Department.java
Create a new Java file Department.java under the package com.javainterviewpoint and add the following code
package com.javainterviewpoint; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="DEPARTMENT") public class Department { @Id @Column(name="DEP_ID") @GeneratedValue private int depId; @Column(name="DEP_NAME") private String depName; @ManyToMany(mappedBy="department") private Set<Employee> employee; public Department() { super(); } public Department(int depId, String depName, Set employee) { super(); this.depId = depId; this.depName = depName; this.employee = employee; } public int getDepId() { return depId; } public void setDepId(int depId) { this.depId = depId; } public String getDepName() { return depName; } public void setDepName(String depName) { this.depName = depName; } public Set<Employee> getEmployee() { return employee; } public void setEmployee(Set<Employee> employee) { this.employee = employee; } }
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="Employee" /> <mapping class="Department" /> </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 mapping file for which we need the table to be created or updated.
<mapping class="Employee" /> <mapping class="Department" />
Hibernate Many-To-Many Example
package com.javainterviewpoint; import java.util.HashSet; 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 HibernateManyToMany { 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 two Employee Objects Employee employee1 = new Employee(); employee1.setEmpName("Employee 1"); Employee employee2 = new Employee(); employee2.setEmpName("Employee 2"); //Create two Department Objects Department department1 = new Department(); department1 .setDepName("Mechanical Department"); Department department2 = new Department(); department2 .setDepName("Electrical Department"); Set s = new HashSet(); s.add(department1); s.add(department2); //Set Department into Employee employee1.setDepartment(s); employee2.setDepartment(s); //Save the Employee object session.save(employee1); session.save(employee2); //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 two Employee objects and set values to its properties
Employee employee1 = new Employee(); employee1.setEmpName("Employee 1"); Employee employee2 = new Employee(); employee2.setEmpName("Employee 2");
- Create two Department objects and set value to it properties
Department department1 = new Department(); department1 .setDepName("Mechanical Department"); Department department2 = new Department(); department2 .setDepName("Electrical Department");
- Create a Set and add the two department objects into it. Finally, add the Set into department property of Employee
Set s = new HashSet(); s.add(department1); s.add(department2); employee1.setDepartment(s); employee2.setDepartment(s);
- save() method of the session object will persist the employee object into the database. Since we have used cascade as all it inturn saves the department.
session.save(employee1); session.save(employee2);
- Finally get the transaction and commit the changes and close the session.
session.getTransaction().commit(); session.close();
Console:
INFO: HHH000261: Table found: DEPARTMENT Dec 29, 2016 2:42:54 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [dep_name, dep_id] Dec 29, 2016 2:42:54 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] Dec 29, 2016 2:42:54 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014864] Dec 29, 2016 2:42:56 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: EMPLOYEE Dec 29, 2016 2:42:56 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [emp_name, emp_id] Dec 29, 2016 2:42:56 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] Dec 29, 2016 2:42:56 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014862] Dec 29, 2016 2:42:59 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: EMPLOYEE_DEPARTMENT Dec 29, 2016 2:42:59 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [dep_id, emp_id] Dec 29, 2016 2:42:59 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [fk_dep_id, fk_emp_id] Dec 29, 2016 2:42:59 PM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [sys_c0014867] Dec 29, 2016 2:42:59 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: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into EMPLOYEE (EMP_NAME, EMP_ID) values (?, ?) Hibernate: insert into DEPARTMENT (DEP_NAME, DEP_ID) values (?, ?) Hibernate: insert into DEPARTMENT (DEP_NAME, DEP_ID) values (?, ?) Hibernate: insert into EMPLOYEE (EMP_NAME, EMP_ID) values (?, ?) Hibernate: insert into EMPLOYEE_DEPARTMENT (EMP_ID, DEP_ID) values (?, ?) Hibernate: insert into EMPLOYEE_DEPARTMENT (EMP_ID, DEP_ID) values (?, ?) Hibernate: insert into EMPLOYEE_DEPARTMENT (EMP_ID, DEP_ID) values (?, ?) Hibernate: insert into EMPLOYEE_DEPARTMENT (EMP_ID, DEP_ID) values (?, ?)
Retrieving Employee and Department
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 RetriveData { 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("*** Retrieving Department 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()); System.out.println("** Department Details **"); Set deparmentSet = employee.getDepartment(); for(Department department : deparmentSet) { System.out.println("Department Id : "+department.getDepId()); System.out.println("Department Name : "+department.getDepName()); System.out.println(""); } } System.out.println("*** Retrieving Employee through Department *** "); List depList = session.createQuery("from Department").list(); for(Department department : depList) { System.out.println("** Department Details **"); System.out.println("Department Id : "+ department.getDepId()); System.out.println("Department Name : "+ department.getDepName()); System.out.println("** Employee Details **"); Set employeeSet = department.getEmployee(); for(Employee employee : employeeSet) { System.out.println("Employee Id : "+ employee.getEmpId()); System.out.println("Employee Name : "+ employee.getEmpName()); System.out.println(""); } } //Close the session session.close(); } }
Output:
The above code shows that we can retrieve the Departments through Employee and vice-versa.
*** Retrieving Department through Employee *** Hibernate: select employee0_.EMP_ID as EMP_ID1_1_, employee0_.EMP_NAME as EMP_NAME2_1_ from EMPLOYEE employee0_ ** Employee Details ** Employee Id : 286 Employee Name : Employee 1 ** Department Details ** Hibernate: select department0_.EMP_ID as EMP_ID1_1_0_, department0_.DEP_ID as DEP_ID2_2_0_, department1_.DEP_ID as DEP_ID1_0_1_, department1_.DEP_NAME as DEP_NAME2_0_1_ from EMPLOYEE_DEPARTMENT department0_ inner join DEPARTMENT department1_ on department0_.DEP_ID=department1_.DEP_ID where department0_.EMP_ID=? Department Id : 288 Department Name : Mechanical Department Department Id : 287 Department Name : Electrical Department ** Employee Details ** Employee Id : 289 Employee Name : Employee 2 ** Department Details ** Hibernate: select department0_.EMP_ID as EMP_ID1_1_0_, department0_.DEP_ID as DEP_ID2_2_0_, department1_.DEP_ID as DEP_ID1_0_1_, department1_.DEP_NAME as DEP_NAME2_0_1_ from EMPLOYEE_DEPARTMENT department0_ inner join DEPARTMENT department1_ on department0_.DEP_ID=department1_.DEP_ID where department0_.EMP_ID=? Department Id : 288 Department Name : Mechanical Department Department Id : 287 Department Name : Electrical Department *** Retrieving Employee through Department *** Hibernate: select department0_.DEP_ID as DEP_ID1_0_, department0_.DEP_NAME as DEP_NAME2_0_ from DEPARTMENT department0_ ** Department Details ** Department Id : 287 Department Name : Electrical Department ** Employee Details ** Hibernate: select employee0_.DEP_ID as DEP_ID2_0_0_, employee0_.EMP_ID as EMP_ID1_2_0_, employee1_.EMP_ID as EMP_ID1_1_1_, employee1_.EMP_NAME as EMP_NAME2_1_1_ from EMPLOYEE_DEPARTMENT employee0_ inner join EMPLOYEE employee1_ on employee0_.EMP_ID=employee1_.EMP_ID where employee0_.DEP_ID=? Employee Id : 289 Employee Name : Employee 2 Employee Id : 286 Employee Name : Employee 1 ** Department Details ** Department Id : 288 Department Name : Mechanical Department ** Employee Details ** Hibernate: select employee0_.DEP_ID as DEP_ID2_0_0_, employee0_.EMP_ID as EMP_ID1_2_0_, employee1_.EMP_ID as EMP_ID1_1_1_, employee1_.EMP_NAME as EMP_NAME2_1_1_ from EMPLOYEE_DEPARTMENT employee0_ inner join EMPLOYEE employee1_ on employee0_.EMP_ID=employee1_.EMP_ID where employee0_.DEP_ID=? Employee Id : 289 Employee Name : Employee 2 Employee Id : 286 Employee Name : Employee 1
Leave a Reply