Featured Article

Master Spring Boot JPA Relationships with Practical Example Code

Kenneth Jul 13, 2026

Spring Boot, a popular Java-based framework, simplifies application development by providing a portable, production-ready platform. When it comes to database operations, Spring Boot's integration with Java Persistence API (JPA) makes it a readable and efficient choice. Let's delve into an example of how to manage relationships in Spring Boot using JPA.

Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers
Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers

Before we dive into the relationship examples, let's set the stage. Suppose we have two entities, Customer and Order, and we want to establish a one-to-many relationship between them. You might have already set up your project with Spring Boot and JPA (Hibernate), with your entities mapped using annotations like '@Entity', '@Id', and '@GeneratedValue'.

Red Hunter Boots Outdoors, Creative Flower Arrangement Ideas, Red Rain Boots With Flowers, Spring Floral Rain Boots, Spring Garden Photography Tips, Gardener Aesthetic, Hunter Short Boots Outfit, Hunters Boots, Rain Boots And Flowers
Red Hunter Boots Outdoors, Creative Flower Arrangement Ideas, Red Rain Boots With Flowers, Spring Floral Rain Boots, Spring Garden Photography Tips, Gardener Aesthetic, Hunter Short Boots Outfit, Hunters Boots, Rain Boots And Flowers

Defining the One-to-Many Relationship

In our example, one customer can place many orders, but one order is placed by one customer. Thistranslate to a one-to-many relationship, with 'Customer' being the parent entity and 'Order' being the child entity.

couple pictures
couple pictures

To define this relationship, we'll use JPA annotations in both entities. In the Customer entity, we'll use the '@OneToMany' annotation, while in the Order entity, we'll use the '@ManyToOne' annotation.

Customer Entity

Spring Boot Annotations Cheat Sheet (Save This!)
Spring Boot Annotations Cheat Sheet (Save This!)

In the Customer entity, we'll create a list to store the orders and map the relationship using '@OneToMany'.

The '@JoinColumn' annotation in the 'orders' field will establish the foreign key mapping in the database table for 'Customer'.

Order Entity

Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot Project Ideas for Resume | Real-World Examples

In the Order entity, we'll map the relationship to the 'customer' field using '@ManyToOne'. The '@JoinColumn' annotation will define the foreign key column name in the database table for 'Order'.

Here's a simplified example of how the entities might look:

```java @Entity public class Customer { @Id @GeneratedValue private Long id; private String name; @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true) private List orders = new ArrayList<>(); // Getters and setters } @Entity public class Order { @Id @GeneratedValue private Long id; private String product; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "customer_id") private Customer customer; // Getters and setters } ```

Using the Relationship in Spring Data JPA

Entry Point of a Springboot Application
Entry Point of a Springboot Application

Now that our entities are defined, let's see how to use this relationship in Spring Data JPA through our repository and service layers.

In our CustomerRepository, we'll find that Spring Data JPA automatically creates methods that utilize our relationship. For example, we can find customers by their orders:

Cute bouquet in yellow rubber boots. Gardening boot with flowers. Spring Concept
Cute bouquet in yellow rubber boots. Gardening boot with flowers. Spring Concept
Boot scootin’ lover 🖤

@boot_barn_official 

#westernstyle #cowboyboots #westernlifeandstyle #cowgirlstyle #bootbarn 

Inspired by @lacey__szczepanik 🥀 Boot Barn, Ceremony Music, Cowgirl Style, Awards Ceremony, Western Fashion, Festival, Boots
Boot scootin’ lover 🖤 @boot_barn_official #westernstyle #cowboyboots #westernlifeandstyle #cowgirlstyle #bootbarn Inspired by @lacey__szczepanik 🥀 Boot Barn, Ceremony Music, Cowgirl Style, Awards Ceremony, Western Fashion, Festival, Boots
How To Reduce Boilerplate Code In Java : Lombok API
How To Reduce Boilerplate Code In Java : Lombok API
Pink Gardening Outfit, Love Shack Fancy Hunter Boots, Loveshackfancy Hunter Boots, Hunter Boot Outfit, Pink Hunter Boots With Flowers, Light Pink Hunter Boots, Wellies Outfit Summer, Pink Hunter Boots Outfit, Pink Wellies Outfit
Pink Gardening Outfit, Love Shack Fancy Hunter Boots, Loveshackfancy Hunter Boots, Hunter Boot Outfit, Pink Hunter Boots With Flowers, Light Pink Hunter Boots, Wellies Outfit Summer, Pink Hunter Boots Outfit, Pink Wellies Outfit
a man and woman holding hands walking down a dirt road in front of some trees
a man and woman holding hands walking down a dirt road in front of some trees
Creativity in Doses: A Dose of Spring (finally!) Dress With High Socks, What To Wear Knee High Socks With, Fall Fashion Knee-highs, Spring Synthetic Knee-high Boots, Synthetic Knee-high Boots For Spring, Spring Knee-high Tights, Casual Knee-high Legwear For Spring, Casual Spring Knee-high Boots With Closed Toe, Socks With Boots And Dress
Creativity in Doses: A Dose of Spring (finally!) Dress With High Socks, What To Wear Knee High Socks With, Fall Fashion Knee-highs, Spring Synthetic Knee-high Boots, Synthetic Knee-high Boots For Spring, Spring Knee-high Tights, Casual Knee-high Legwear For Spring, Casual Spring Knee-high Boots With Closed Toe, Socks With Boots And Dress
a pair of green rain boots with flowers in front of a wooden fence and field
a pair of green rain boots with flowers in front of a wooden fence and field
Starting New Relationships
Starting New Relationships
15 Spring Boots Outfits for Stylish Everyday Looks 2026 Guide!
15 Spring Boots Outfits for Stylish Everyday Looks 2026 Guide!

```java interface CustomerRepository extends JpaRepository { List findByOrdersIn(List orders); } ```

Creating and Assigning Orders to a Customer

When creating a new customer, we can also create new orders and assign them to the customer using the 'orders' list in the Customer entity.

Here's how we might do this in a service layer:

```java @Service public class CustomerService { @Autowired private CustomerRepository customerRepository; public Customer createCustomerWithOrders(List orderProducts) { Customer customer = new Customer("John Doe"); saveOrdersAndAssignToCustomer(customer, orderProducts); return customerRepository.save(customer); } private void saveOrdersAndAssignToCustomer(Customer customer, List orderProducts) { List orders = orderProducts.stream() .map(product -> new Order(product)) .collect(Collectors.toList()); customer.getOrders().addAll(orders); for (Order order : orders) { order.setCustomer(customer); } customerRepository.save(customer); } } ```

With this setup, our customer can have many orders, and we can easily manage this relationship in our application. This is just one example of how to manage relationships in Spring Boot using JPA. Other relationship types like many-to-many or one-to-one follow a similar pattern.

In the world of application development, understanding and managing relationships effectively is key to building robust and scalable systems. Spring Boot's integration with JPA makes this process intuitive and efficient, allowing developers to focus on building business logic rather than wrestling with infrastructure.