1. One-to-Many Mapping
Concept
A One-to-Many relationship means one entity is associated with multiple instances of another entity.
Example:
One Department has many Employees.
Department (1) ---- (N) Employee
Database Structure
DEPARTMENT
- id (PK)
- name
EMPLOYEE
- id (PK)
- name
- department_id (FK)
Entity Mapping (Recommended: Bidirectional)
Department Entity
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(
mappedBy = "department",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Employee> employees = new ArrayList<>();
}
Employee Entity
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
Key Points
mappedBy defines the owning side
@ManyToOne is always the owning side
cascade = CascadeType.ALL propagates operations
orphanRemoval = true deletes unlinked children
Foreign key is stored in the many-side table
Common Use Cases
Order → OrderItems
Department → Employees
Blog → Posts
2. Many-to-Many Mapping
Concept
A Many-to-Many relationship means multiple entities can be associated with multiple entities.
Example:
A Student can enroll in many Courses, and a Course can have many Students.
Student (N) ---- (N) Course
Database Structure
STUDENT
- id (PK)
- name
COURSE
- id (PK)
- title
STUDENT_COURSE
- student_id (FK)
- course_id (FK)
Entity Mapping (Bidirectional)
Student Entity
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses = new HashSet<>();
}
Course Entity
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToMany(mappedBy = "courses")
private Set<Student> students = new HashSet<>();
}
Key Points
Hibernate creates a join table
Only one side should define @JoinTable
Use Set to avoid duplicates
Avoid CascadeType.REMOVE (can cause unintended deletions)
3. Best Practices
One-to-Many
✔ Prefer bidirectional mapping
✔ Use List or Set depending on order/uniqueness
✔ Keep foreign key on the many-side
Many-to-Many
✔ Avoid if relationship has extra attributes
✔ Use a join entity instead if needed
4. When to Use a Join Entity Instead of Many-to-Many
If the relationship has additional fields, convert it into two One-to-Many relationships.
Example: Enrollment with grade
Student → Enrollment ← Course
This is more flexible and scalable.
5. Summary Table
Feature One-to-Many Many-to-Many
Relationship 1 → N N ↔ N
Foreign Key In many-side table Join table
Join Table No Yes
Complexity Simple More complex
Extra Fields Easy Requires join entity
Learn Full Stack JAVA Course in Hyderabad
Read More
Using JPA and Hibernate for ORM
Connecting Java Applications to MySQL
CRUD Operations in MySQL for Java Developers
Introduction to Relational Databases
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments