CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > StackOverflowError in JPA Mapping @ManyToMany with @Data Annotation

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

StackOverflowError in JPA Mapping @ManyToMany with @Data Annotation

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

The java.lang.StackOverflowError occurs when there is an infinite recursion or circular reference in our object mappings. In the case of a @ManyToMany with the @Data annotation, the StackOverflowError in JPA mapping can occur due to the bidirectional relationship between entities. This causes an infinite loop during serialization or when accessing certain properties.

Today, one of our fresher team member asked me about this error and I found that she has used @Data annotation from the Lombok project.

To fix the java.lang.StackOverflowError in JPA mapping while using @ManyToMany with @Data annotation, we can try the following approaches:

Table of Contents

Break the Circular Reference
Use @ToString.Exclude
Use @Getter and @Setter
Use a DTO (Data Transfer Object)

Break the Circular Reference

  • One common cause of the StackOverflowError is a circular reference between the entities involved in the @ManyToMany relationship.
  • To break the circular reference, you can mark one side of the relationship with the @JsonIgnore annotation (from com.fasterxml.jackson.annotation package) to exclude it from serialization and avoid the infinite loop.
  • For example, consider two entities EntityA and EntityB with a @ManyToMany relationship:
@Entity
public class EntityA {
    // Other fields and annotations

    @ManyToMany
    @JsonIgnore
    private List<EntityB> entityBs;
    
    // Getters and setters
}

@Entity
public class EntityB {
    // Other fields and annotations

    @ManyToMany(mappedBy = "entityBs")
    private List<EntityA> entityAs;

    // Getters and setters
}

In this example, the @JsonIgnore annotation is applied to the entityBs field in EntityA, which prevents it from being serialized and avoids the circular reference.

Use @ToString.Exclude

  • If you are using the Lombok @Data annotation to generate getters, setters, equals(), hashCode(), and toString() methods, you can use the @ToString.Exclude annotation to exclude the problematic fields from the toString() method generation.
  • By excluding the fields causing the circular reference from the generated toString() method, you can prevent the StackOverflowError during serialization.
@Entity
@Data
public class EntityA {
    // Other fields and annotations

    @ManyToMany
    @ToString.Exclude
    private List<EntityB> entityBs;
}

@Entity
@Data
public class EntityB {
    // Other fields and annotations

    @ManyToMany(mappedBy = "entityBs")
    @ToString.Exclude
    private List<EntityA> entityAs;
}

Use @Getter and @Setter

  • The another option would be to use @Getter and @Setter annonation only instead of @Data.
@Entity
@Getter
@Setter
public class EntityA {
    // Other fields and annotations

    @ManyToMany
    private List<EntityB> entityBs;
}

@Entity
@Getter
@Setter
public class EntityB {
    // Other fields and annotations

    @ManyToMany(mappedBy = "entityBs")
    private List<EntityA> entityAs;
}
  • This does not add other than Getter and Methods.

Use a DTO (Data Transfer Object)

  • If the circular reference cannot be easily broken or excluded, you can create a separate DTO class to represent the data without the circular reference.
  • The DTO class should have only the necessary fields and exclude the circular reference properties.
  • Convert the entities to DTOs before serialization and use the DTOs for serialization.
  • This approach provides more control over the serialization process and allows you to tailor the data specifically for the serialization needs.

By applying one of these approaches, you should be able to fix the java.lang.StackOverflowError caused by circular references in the @ManyToMany mapping when using the @Data annotation.

Do you know how I solved the problem I’ve mentioned above?

I just ask her to use the annotation @ToString.Exclude in the field that was causing the issue and the problem was solved.

In this post, I’ve mentioned the three best methods to solve the java.lang.StackOverflowError caused by circular references in the @ManyToMany mapping when using the @Data annotation. You can use one of them to solve your error.

Cheers!

Related Posts:

  • Recursion in Java
  • Control Statements in Java
  • Spring Data JPA Projection
  • How to Solve “systemctl command not found” Error: A…
  • Spring Boot Circular Dependency Error
  • (Solved) Java 8 date/time types are not supported by default
Tags:javajpalombok
Was this article helpful?
← Previous ArticleDeclaration in Java
Next Article →Install Tomcat

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap