Hibernate one to many example using XML mapping relationship betwen entities. Hibernate one to many relationship parent child using XMl mapping.
1. Overview of Hibernate One to Many Example
Well, one-to-many is one of the types of relationships in Hibernate. This is a parent-child relationship where one parent can have many children. Here, you have to establish a relationship between two different entity/domain model classes.
For your dependency library, you can download the source code of hello world hibernate program, at the end of this post you can find a link to download source code.
2. What is Hibernate one-to-many Relationship
In one to many relationships, one parent can have multiple children. when you read the relationship read from parent to child. In this post, tried to demonstrate one department can have multiple employee. so in Departement table has one foreign key which hold primary key of employee. So when you select a department by you can get many employees.
This can be achieved by two different ways:
- one by foreign key and
- another way by a mapping table.
In this post, tried to demonstrate by the foreign key. However, you can try with mapping table between two entities.
3. Domain model for Hibernate one to many mapping
Here, Department can have many Employee and in POJO Department object holds many reference of employee.
File: Department.java
package com.mysoftkey.relationship.one2many; import java.util.HashSet; import java.util.Set; public class Department { private Long departmentId; private String departmentName; private Setemployees; // utility method to add employee in department public void addEmployee(Employee e){ if (employees == null){ employees = new HashSet (); } } //Gernerate setter/getter , omited to clear the code }
File: Employee.java
package com.mysoftkey.relationship.one2many; import java.sql.Date; public class Employee { private Long employeeId; private String firstname; private String lastname; private Date birthDate; private String cellphone; private Department department; public Employee() { } public Employee(String firstname, String lastname, Date birthdate, String phone) { this.firstname = firstname; this.lastname = lastname; this.birthDate = birthdate; this.cellphone = phone; } public Employee(String firstname, String lastname, String phone) { this.firstname = firstname; this.lastname = lastname; this.cellphone = phone; } @Override public boolean equals(Object emp) { if(emp instanceof Employee) { Employee employee = (Employee)emp; if(this.firstname.equals(employee.getFirstname()) && this.lastname.equals(employee.getLastname())) return true; } return false; } @Override public int hashCode() { return this.firstname.hashCode() + this.lastname.hashCode(); } //Gernerate setter/getter , omited to clear the code }
4. .hbm.xml mapping for Hibernate one-to-many relationship
In this section of .hbm.xml mapping you learn about how to configure one to many mapping in Hibernate using XML.
File: Department.hbm.xml
File: Employee.hbm.xml
If you look first Department.hbm.xml
, its one to many relatioship . if you look Employee.hbm.xml
first , then you find many to one because many employee can have in one department.
5. Configuration for Hibernate one to many relationship
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/hibernate_example root root 2 org.hibernate.dialect.MySQLDialect true create
6. Run one to many relationship
File: OneToManyClient.java
package com.mysoftkey.relationship.one2many; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * This example is used to demonstrate one to many example in Hibenate. * * @author Ranjeet Jha * */ public class OneToManyClient { public static void main(String[] args) { // in project use commented code but in demo you can use any one. // SessionFactory sf = HibernateUtil.getSessionFactory(); SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); // Create Department object and populate values Department department = new Department(); department.setDepartmentName("Sales"); // save department session.save(department); // Create list of employee object and assigned department Employee emp1 = new Employee("Ranjeet", "Kumar", "111"); Employee emp2 = new Employee("Tanisha Jha", "Jha", "222"); // assigned saved department to employee to add foreing key in employee table. emp1.setDepartment(department); emp2.setDepartment(department); session.save(emp1); session.save(emp2); session.getTransaction().commit(); session.close(); } }
7. Output of one to many Hibernate relationship
Eclipse console output:
Hibernate: insert into DEPARTMENT_one2many (DEPT_NAME) values (?) Hibernate: insert into EMPLOYEE_one2many (firstname, lastname, birth_date, cell_phone, department_id) values (?, ?, ?, ?, ?) Hibernate: insert into EMPLOYEE_one2many (firstname, lastname, birth_date, cell_phone, department_id) values (?, ?, ?, ?, ?)
mySQL output:
mysql> select * from employee_one2many -> ; +-------------+-------------+----------+------------+------------+---------------+ | EMPLOYEE_ID | firstname | lastname | birth_date | cell_phone | department_id | +-------------+-------------+----------+------------+------------+---------------+ | 1 | Ranjeet | Kumar | NULL | 111 | 1 | | 2 | Tanisha Jha | Jha | NULL | 222 | 1 | +-------------+-------------+----------+------------+------------+---------------+ 2 rows in set (0.00 sec) mysql> select * from department_one2many; +---------------+-----------+ | DEPARTMENT_ID | DEPT_NAME | +---------------+-----------+ | 1 | Sales | +---------------+-----------+ 1 row in set (0.00 sec) mysql>
8. Reference
Thanks for visiting this post for learning Hibernate one to may relationship example. You can also visit Java Hibernate ORM Tutorial Listing page for more articles on Hibernate ORM framework.
Happy Learning 🙂 for Hibernate one to many mapping examples.
Your comments are welcome to improve this one-to-many relationship in Hibernate post. Happy Learning 🙂