Hibernate – one-to-many relationship example

Connect with

Well, one-to-many is the type of relationship in Hibernate. This is parent child relationship where one parent can have many child. Here, you have to establishe relationship between two different entity/domain model class.

For your dependency library you can dwonload source code of hello world hibernate program, at the end of this post you can find link to download source code.

1. What is one-to-many Relationship

In one to many relateionship, one parent can have multiple childs. 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 department by you can get many employee.

This can be achieved by two different ways:

  1. one by foriegn key and
  2. another way by mapping table.

In this post, tried to demonstrate by foriegn key. However, you can try with mapping table between two entities.

2. POJO/domain model class

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 Set employees;

	// 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;
	public boolean equals(Object emp) {
	  if(emp instanceof Employee) {
	     Employee employee = (Employee)emp;
	     if(this.firstname.equals(employee.getFirstname()) &&
		return true;

	 return false;
	public int hashCode() {
	  return this.firstname.hashCode() + this.lastname.hashCode();
	//Gernerate setter/getter , omited to clear the code	

3. .hbm.xml mapping for one-to-many relationship

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.

4. Hibernate Configuration for one to many relationship


5. 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();

    // Create Department object and populate values
    Department department = new Department();
    // 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.


6. 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;
|             1 | Sales     |
1 row in set (0.00 sec)


Your comments are welcome to improve this one-to-many relationship in Hibernate post. Happy Learning 🙂

Connect with

Leave a Reply

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