JPA Hibernate One to Many Bidirectional Mapping Example

JPA Many to Many example with Hibernate in Spring Boot
In this tutorial, we will learn how to implement step-by-step one-to-many bidirectional entity mapping using JPA and Hibernate, and the MySQL database. JPA Hibernate One to Many Bidirectional Mapping Example
JPA Many to Many example with Hibernate in Spring Boot
The @OneToMany and @ManyToOne JPA annotations are used to link one-to-many bidirectional entity mapping.

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.
In this example, we implement step-by-step one-to-many bidirectional entity mapping using JPA and Hibernate, and the MySQL database. JPA Hibernate One to Many Bidirectional Mapping Example
Teacher Class Entity
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'
 

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/haider spring.datasource.username=root spring.datasource.password=



Sure, here are the application properties that you need to set to connect to a MySQL database:

  • spring.jpa.database-platform: The name of the database platform. In this case, it is MySQL.
  • spring.jpa.hibernate.ddl-auto: The strategy for creating or updating the database schema. In this case, it is update, which means that Spring Boot will update the schema to match the entities in your application.
  • spring.datasource.driverClassName: The fully qualified name of the JDBC driver for MySQL.
  • spring.datasource.url: The JDBC URL for connecting to the MySQL database.
  • spring.datasource.username: The username for connecting to the MySQL database.
  • spring.datasource.password: The password for connecting to the MySQL database.

Once you have set these properties, you can start your Spring Boot application and it will connect to the MySQL database.

Here are some additional tips for connecting to a MySQL database:

  • If you are using a different database platform, you will need to change the spring.jpa.database-platform property to the name of the platform that you are using.
  • If you are using a different version of MySQL, you may need to change the spring.datasource.driverClassName property to the fully qualified name of the JDBC driver for the version of MySQL that you are using.
  • If you are using a different username or password for connecting to the MySQL database, you will need to change the spring.datasource.username and spring.datasource.password properties to the username and password that you are using.
 

Leave a Reply

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