JPA/Hibernate One to Many Bidirectional Mapping Example
There are two types of one-to-many associations –
- Unidirectional → In this type of association, only the source entity has a relationship field that refers to the target entity. We can navigate this type of association from one side.
- Bidirectional → In this type of association, each entity (i.e. source and target) has a relationship field that refers to each other. We can navigate this type of association from both sides.
package com.onetomany.demoonetomany.Teacher; import com.onetomany.demoonetomany.Course.Course; import javax.persistence.*; import java.util.List; @Entity public class Teacher { @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; String name; String DepartName; @OneToMany(mappedBy = "teacher",cascade = CascadeType.ALL) List<Course> courseList; public List<Course> getCourseList() { return courseList; } public void setCourseList(List<Course> courseList) { this.courseList = courseList; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartName() { return DepartName; } public void setDepartName(String departName) { DepartName = departName; } }
Course Entity Class
package com.onetomany.demoonetomany.Course; import com.onetomany.demoonetomany.Teacher.Teacher; import javax.persistence.*; @Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; String courseName; @ManyToOne Teacher teacher; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }
Course Repository
package com.onetomany.demoonetomany.Course; import org.hibernate.cfg.JPAIndexHolder; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface CourseRespository extends JpaRepository<Course,Integer> { } Teacher Repository
package com.onetomany.demoonetomany.Teacher; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface TeacherRespository extends JpaRepository<Teacher,Integer> { }
application.properties spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password= ControllerST Conrtorller class
package com.onetomany.demoonetomany.API; import com.onetomany.demoonetomany.Course.Course; import com.onetomany.demoonetomany.Course.CourseRespository; import com.onetomany.demoonetomany.Teacher.Teacher; import com.onetomany.demoonetomany.Teacher.TeacherRespository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController public class ControllerST { @Autowired CourseRespository courseRespository; @Autowired TeacherRespository teacherRespository; @PostMapping("/add/teacher") public void addrecord(Teacher teacher){ List<Course> courseList=new ArrayList<>(); Course course=new Course(); course.setCourseName("Hacking Course"); course.setTeacher(teacher); courseList.add(course); teacher.setName("Waseem Haider Hacker"); teacher.setDepartName("IT Department"); teacher.setCourseList(courseList); teacherRespository.save(teacher); } @GetMapping("/get/list") public List<Teacher> getlistadd(){ return teacherRespository.findAll(); } }
Main Class
package com.onetomany.demoonetomany; import com.onetomany.demoonetomany.Course.Course; import com.onetomany.demoonetomany.Teacher.Teacher; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.ArrayList; import java.util.List; @SpringBootApplication public class DemoonetomanyApplication { public static void main(String[] args) { SpringApplication.run(DemoonetomanyApplication.class, args); } } Dependence of Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'mysql:mysql-connector-java' testImplementation 'org.springframework.boot:spring-boot-starter-test'