Adding the JPA repository
3.1 ManufactureRepo
This Interface contains the forms the bridge of entity Manufactures to the Database
package com.example.Mapping.Repositories;
import com.example.Mapping.Models.Manufactures;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Spring Data JPA repository for the Manufacture entity.
* This interface extends JpaRepository and provides basic
* CRUD operations for Manufactures entities.
*/
public interface ManufacturesRepo extends JpaRepository<Manufactures, Integer> {
}
3.2 ModelRepo
This Interface contains the forms the bridge of entity Model to the Database
package com.example.Mapping.Repositories;
import com.example.Mapping.Models.Model;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Spring Data JPA repository for the Model entity.
* This interface extends JpaRepository and provides basic
* CRUD operations for Model entities.
*/
public interface ModelRepo extends JpaRepository<Model, Integer> {
}
3.3 MappingApplication
Driver class for Spring Boot application to test the One-to-One Mapping of hibernate.
- This Spring Boot application initializes and saves data using JPA repositories.
- It creates a Manufacturer(“Honda”) entity and two associated Model (“AYZ” and “ZET”) entities with a one-to-many relationship.
- The data is then stored in corresponding repositories during the application’s run.
package com.example.Mapping;
import com.example.Mapping.Models.Manufactures;
import com.example.Mapping.Models.Model;
import com.example.Mapping.Repositories.ManufacturesRepo;
import com.example.Mapping.Repositories.ModelRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot application for mapping relationships between Manufactures and Models.
*/
@SpringBootApplication
public class MappingApplication implements CommandLineRunner {
// Injecting Manufactures and Model repositories using autowiring
@Autowired
private ManufacturesRepo manufacturesRepo;
@Autowired
private ModelRepo modelRepo;
public static void main(String[] args) {
SpringApplication.run(MappingApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Creating a new Manufactures object with ID 1 and name "Honda"
Manufactures data = new Manufactures(1, "Honda");
// Saving the Manufactures record
manufacturesRepo.save(data);
// Creating two new Model objects associated with the "Honda" manufacturer
Model model1 = new Model(1, "AYZ", data);
Model model2 = new Model(2, "ZET", data);
// Saving the Model records
modelRepo.save(model1);
modelRepo.save(model2);
}
}
Run the main application:
Console log of project running.
Manufacture Table
Database view of manufactures table:
Model Table
Database view of model table:
The below image depicts the link between the two tables.
Hibernate – One-to-Many Mapping
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.