Hibernate is used to increase the data manipulation efficiency between the spring application and the database, insertion will be done is already defined with the help of hibernating.
- JPA (Java persistence API) is like an interface and hibernate is the implementation of the methods of the interface.
In this article, we will discuss One-to-Many Mapping in the Hibernate. Let's understand the One-to-many mapping with the help of a real-life example. Bike manufacturers can manufacture multiple models of the bike, but the same bike model cannot be manufactured by multiple manufacturers.
Syntax:
This mapped variable of the other tables is responsible for mapping between two tables.
@oneToMany(mappedby=”nameofmappedvariable”)
For this example, we will map the id field of manufactures to the manufacture_id of the model entity.
1. Configuration of Hibernate One-to-Many Mapping
1.1 Go to this spring initializr and fill in the details mentioned.
- Project: Maven
- Language: Java
- Spring Boot: 2.5.7
- Packaging: JAR
- Java: 11
- Dependencies:
- Spring Web
- Spring Data JPA
- MySql Driver
Click on Generate which will download the starter project.
1.2 Extract the zip file
Now open a suitable IDE and then go to File > New > Project from existing sources > Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync as pictorially depicted below as follows:
1.3: Adding the necessary properties in the application.properties file
Database configuration required for connection of Mysql database to the Spring Boot Application.
spring.datasource.username=root
spring.datasource.password=Aayush
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.jpa.hibernate.ddl-auto=update
1.4 Folder directory creation
Go to src > main > java > com > example > Mapping and create two files in the models folder i.e Manufactures.java and Model.java
2. Implementing the Application
Over here, we will implement the
- Entity - Manufacures, Model
- Repository - ManufacuresRepo, ModelRepo
- Application class
2.1 Manufactures.java
This contains the entity Manufactures, which contains the required associated fields.
- id - int type
- manufactures_name - string type
- models - mapped with one-to-many mapping
- constructors, getters, setters, etc.
package com.example.Mapping.Models;
import javax.persistence.*;
import java.util.List;
@Entity
public class Manufactures {
// Unique identifier for the manufacturer
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
// Name of the manufacturer
private String manufactures_name;
// A manufacturer can have many models
@OneToMany(mappedBy = "manufacturer")
private List<Model> models;
// Constructor with both id and name
public Manufactures(int id, String manufactures_name) {
this.id = id;
this.manufactures_name = manufactures_name;
}
// Default constructor
public Manufactures() {
}
// Getters and setters for id and name
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getManufactures_name() {
return manufactures_name;
}
public void setManufactures_name(String manufactures_name) {
this.manufactures_name = manufactures_name;
}
// Getter for models
public List<Model> getModels() {
return models;
}
// Setter for models
public void setModels(List<Model> models) {
this.models = models;
}
}