DEV Community

Cover image for How to Implement Entity Relationships in Spring Boot
Jean
Jean

Posted on

How to Implement Entity Relationships in Spring Boot

In real-world applications, data rarely lives alone in its own .

Users place orders, books have authors, students enroll in courses — and in my case, customers fill carts and place purchases.

These connections between pieces of data are modeled using entity relationships in Spring Boot with JPA (Java Persistence API).

Getting these relationships right is key to building robust, scalable applications.

In JPA:

Entities are Java classes that map to database tables.

Each class field represents a column in that table.

We mark a class as an entity using @Entity.

If a field’s name matches the database column name, we’re good to go. If not, we can explicitly set it using @column(name = "custom_column_name").

In this post, We will go through the three main types of entity relationships I commonly used, using examples from my e-commerce REST API project.

🔗 Types of Relationships in Spring Boot

Spring Boot uses JPA annotations to define relationships between entities. Here are the most common types:

1️⃣ One-to-One (@OneToOne)

Each entity has exactly one related entity.

Example: “A cart is assigned to one purchase, and each purchase is associated with one cart.”

To create this relationship, we use the JPA annotation @OneToOne on the corresponding entity property.

     @Entity 
     public class CartEntity { 
     @OneToOne 
     private PurchaseEntity purchase; 
     } 
Enter fullscreen mode Exit fullscreen mode

Nice — the connection is made. Easy, don’t you think? 😊

2️⃣ One-to-Many / Many-to-One (@OneToMany, @ManyToOne)

As the name suggests:

One entity is related to many others, and each of those relates back to one.

Example: One user can have many carts (e.g., past carts, saved carts), but each cart belongs to only one user.

To make this connection, we can either:

Use annotations directly on the entity properties, or

Create a separate entity and table if we want to store additional properties related to the relationship (like total product cost or number of products in the cart).

Here’s an example of the second approach:

@Entity 
public class CartUser { 
@Id
private Long idCartUser; 
@ManyToOne 
@JoinColumn(name = "cart_id") 
private CartEntity cart; 
@ManyToOne 
@JoinColumn(name = "user_id") 
private UserEntity user; 
private Long numberOfProductsInCart; 
private Double totalCost; 
} 
Enter fullscreen mode Exit fullscreen mode

The corresponding database table will have the same properties. If you don’t need extra fields like totalcost or numberofproductincart, you can simply use @OneToMany or @ManyToOne with @JoinColumn directly on the entity — both approaches work correctly.

3️⃣ Many-to-Many (@ManyToMany)

Entities relate to many others in both directions.

Example: One order can contain multiple products, and one product can appear in multiple orders.

We use the @ManyToMany annotation to define this relationship. For convenience, you can either:

Create a new table and entity to store both foreign keys (especially if you need extra fields like quantity or price), or

Use the annotation directly on the appropriate property.

I hope you found this guide useful! If you have additional insights or pro tips, feel free to share them in the comments — I’d love to hear your thoughts. 😊

Top comments (0)