How to Implement One to Many Mapping in Spring Boot?

Last Updated : 28 Jan, 2026

Spring Boot simplifies Java application development by providing auto-configuration, embedded servers, and starter dependencies. For database interaction, Spring Boot commonly uses Spring Data JPA, which is a specification (API), while Hibernate is its most widely used implementation.

What is One-to-Many Mapping?

A One-to-Many relationship means that one parent entity is associated with multiple child entities.

Example:

  • One Student -> Many Addresses
  • A student can have multiple addresses.
  • Each address belongs to only one student.

Step-by-Step Implementation

Step 1: Create a Spring Boot Project

Use Spring Initializr to generate the project.

Project Configuration:

  • Project: Maven
  • Language: Java
  • Spring Boot: 2.5.6
  • Packaging: JAR
  • Java: 11
  • Dependencies: Spring Web,Spring Data JPA, MySql Driver

Click on Generate which will download the starter project.

Step 2: Import the Project into IDE

  • Extract the ZIP file
  • Open IDE -> Import Project from Existing Sources
  • Select pom.xml
  • Wait for Maven dependencies to download

Project structure:

Database table:

Step 3: Configure Database Properties

Add the following to application.properties:

spring.datasource.username=root
spring.datasource.password=Aayush
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.jpa.hibernate.ddl-auto=update

Explanation:

  • mapping: database name
  • ddl-auto=update: automatically creates/updates tables
  • MySQL credentials must match local setup

Step 4: Create Entity Classes

Create a Models package and add the following classes.

Address Entity (Child Table)

Java
package com.example.Mapping.Models;

import javax.persistence.*;

@Entity
@Table(name = "Address")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String cityname;

    @ManyToOne
    @JoinColumn(name = "student_id")
    private StudentInformation student;

    public Address() {}
    public Address(int id, String cityname, StudentInformation student) {
        this.id = id;
        this.cityname = cityname;
        this.student = student;
    }
}

Explanation:

  • @ManyToOne: many addresses belong to one student
  • @JoinColumn: creates a foreign key (student_id) in Address table

StudentInformation Entity (Parent Table)

Java
package com.example.Mapping.Models;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "Student")
public class StudentInformation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int rollno;

    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Address> addresses;

    public StudentInformation() {}

    public StudentInformation(int rollno, String name) {
        this.rollno = rollno;
        this.name = name;
    }
}

Explanation:

  • @OneToMany: one student can have multiple addresses
  • CascadeType.ALL: operations on Student cascade to Address
  • Missing mappedBy means JPA will create an extra join table (Acceptable for learning; not optimal for production)

Step 5: Create Repository Interfaces

Project Structure:

Address Repository:

Java
package com.example.Mapping.Repository;

import com.example.Mapping.Models.Address;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AddressRepo extends JpaRepository<Address, Integer> {
}

Student Repository:

Java
package com.example.Mapping.Repository;

import com.example.Mapping.Models.StudentInformation;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepo extends JpaRepository<StudentInformation, Integer> {
}

Explanation:

  • provides CRUD methods automatically
  • No impleJpaRepository mentation required

Step 6: Insert Data Using CommandLineRunner

MappingApplication:

Java
@SpringBootApplication
public class MappingApplication implements CommandLineRunner {

    @Autowired
    StudentRepo studentRepo;

    @Autowired
    AddressRepo addressRepo;

    public static void main(String[] args) {
        SpringApplication.run(MappingApplication.class, args);
    }

    @Override
    public void run(String... args) {

        StudentInformation student =
            new StudentInformation(1, "Aayush");
        studentRepo.save(student);

        Address address =
            new Address(1, "Sonipat", student);
        addressRepo.save(address);
    }
}

Explanation:

  • CommandLineRunner executes code on application startup
  • Student is saved first
  • Address is linked to the student via foreign key

Now run the main application

Terminal Output:

Database Tables Created StudentTable:

Address Table Address.java:

Comment