Advertisement
Google Ad Slot: content-top
Spring Boot Many-to-One Mapping
Many-to-Many relationships occur when multiple entities relate to multiple entities. Example:
- Students can enroll in multiple Courses
- Courses can have multiple Students
Define Student and Course Entities
We'll create a bidirectional Many-to-Many relationship between Student and Course
✅ This creates a join tablestudent_course to link students and courses.
✅ Bidirectional Mapping: The relationship is mapped from Student to Course.
Table name : student
| id | name |
|---|
Table name : course
| id | name |
|---|
Table name : student_course
| student_id | course_id |
|---|
Create JPA Repositories
Create Services
Create Controllers
Testing the REST API with Postman:
Once the application is running, test the endpoints:
Create Students with Courses POST:http://localhost:8080/students
Create a Student with Existing Courses POST:http://localhost:8080/students/createStudent
Get Student with Courses GET:http://localhost:8080/students/{studentID}
Create Student Only POST:http://localhost:8080/students/createStudentOnly
Add More Courses to Student PUT:http://localhost:8080/students/{studentID}/courses
Delete Courses with Student DELETE:http://localhost:8080/students/{studentID}