Home Many-To-One Relationship
Post
Cancel

Many-To-One Relationship

Many-To-One Relationship:

In a Many To One Relationship, many instances of one entity are related to one instance of another entity. For example, one employee can have many tasks assigned to them. Let’s explore this concept in more detail.

First, let’s consider the entity classes for the Employee and Task entities:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Entity
public class Department {
  @Id
  @GeneratedValue
  private int id;
  private String name;
  // getters and setters
}

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private int id;
  private String name;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "department_id")
  private Department department;

  // getters and setters
}

In this updated example, we have added the @Entity annotation to both the Department and Employee classes to indicate that they are entities that should be mapped to database tables. We have also included the @Id and @GeneratedValue annotations on the id fields to indicate that they are primary keys that should be automatically generated.

On the Employee class, we have added the @ManyToOne annotation on the department field to indicate that there is a Many To One Relationship between the Employee and Department entities. This field holds a reference to the Department object to which the employee belongs.

Save Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Create a new instance of the Department entity
Department itDepartment = new Department();
itDepartment.setName("IT");

Department hrDepartment = new Department();
  hrDepartment.setName("HR");

// Create a new instance of the Employee entity and associate it with the Department
  Employee employee = new Employee();
  employee.setName("John Doe");
  employee.setDepartment(itDepartment);

  Employee employee2 = new Employee();
  employee2.setName("Jane Doe");
  employee2.setDepartment(itDepartment);

  Employee employee3 = new Employee();
  employee3.setName("Bob Smith");
  employee3.setDepartment(hrDepartment);

  Employee employee4 = new Employee();
  employee4.setName("Alice Lee");
  employee4.setDepartment(hrDepartment);

// Get a reference to the Hibernate SessionFactory
  SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

// Open a new Hibernate Session
  Session session = sessionFactory.openSession();

// Begin a new transaction
  Transaction tx = session.beginTransaction();

// Save the Department and Employee entities to the database
  session.save(itDepartment);
  session.save(hrDepartment);
  session.save(employee);
  session.save(employee2);
  session.save(employee3);
  session.save(employee4);

// Commit the transaction
  tx.commit();

// Close the session
  session.close();

Visual Database Table

departments table:
+----+-----------+
| id | name      |
+----+-----------+
| 1  | HR        |
| 2  | IT        |
+----+-----------+

employees table:
+----+-----------+---------------+
| id | name      | department_id |
+----+-----------+---------------+
| 1  | John Doe  | 1             |
| 2  | Jane Doe  | 1             |
| 3  | Bob Smith | 2             |
| 4  | Alice Lee | 2             |
+----+-----------+---------------+

Conclusion

In Hibernate, a many-to-one relationship represents a relationship between two entities where multiple instances of one entity are associated with a single instance of another entity.

This post is licensed under CC BY 4.0 by the author.