Quick revision
The short answer
Lombok's @Data generates equals, hashCode and toString over every field, including the associations. With a bidirectional @OneToMany, the parent's methods call the children's, and the children's call the parent's again, so they recurse until a StackOverflowError, and on the way they touch lazy collections and load them from the database. The safe approach is @Getter and @Setter only, equals and hashCode written by hand on the id or a business key, and toString without associations.
What to remember
In this answer, we saw that @Data generates equals, hashCode and toString over every field, so a bidirectional @OneToMany makes them call each other until the stack overflows, and they load lazy associations along the way. The safe entity uses @Getter and @Setter, a toString with only simple fields, and hand-written equals and hashCode on the id with a constant hash, or on an immutable business key.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Lombok's @Data generates equals, hashCode and toString over every field, including the associations. With a bidirectional @OneToMany, the parent's methods call the children's, and the children's call the parent's again, so they recurse until a StackOverflowError, and on the way they touch lazy collections and load them from the database. The safe approach is @Getter and @Setter only, equals and hashCode written by hand on the id or a business key, and toString without associations.
Why this question is asked
Lombok saves a lot of typing, and @Data is the annotation people reach for first. On plain DTOs it is fine. On JPA entities it turns two ordinary actions, logging an object and putting it in a set, into a crash or a surprise trip through the whole object graph. Interviewers ask this because it tests both Lombok and Hibernate knowledge together. In this answer, we will recap what @Data generates, reproduce the failure, see why it recurses and loads data, and then write a safe entity.
1A Quick Recap of What @Data Generates
@Data is a shortcut for several Lombok annotations at once: @Getter, @Setter, @RequiredArgsConstructor, @ToString and @EqualsAndHashCode. The last two are the dangerous ones here. Lombok's toString prints every non-static field, and its equals and hashCode compare and hash every non-static field. Lombok does not know what a JPA association is; to it, a List<OrderLine> is just another field to include.
A bidirectional association means both sides hold a reference: the Order has a list of lines, and each OrderLine has a field pointing back to its Order. That back reference is what makes the generated methods go in circles.
2Reproducing the Failure
Here are two entities with @Data and a bidirectional association.
Notice that no database is involved yet. The objects are new, and both calls still crash. That tells us the problem is in the generated methods, not in Hibernate.
3Why the Methods Recurse
Follow the toString call. PurchaseOrder.toString() prints its fields, so it calls lines.toString(). The list prints each element, so it calls OrderLine.toString(). OrderLine prints its fields, including order, so it calls PurchaseOrder.toString() again, and we are back at the start. Each round adds stack frames, and after a few thousand rounds the thread's stack is full.
hashCode follows the same path. The order's hash includes the hash of the list, the list's hash combines the hashes of its lines, and each line's hash includes the hash of its order. equals has the same shape when comparing two orders. Any of these can be triggered indirectly: a HashSet calls hashCode, a debugger calls toString to show a variable, and a log statement calls toString even when we only meant to print an id.
7 messages in order. Play them, or step through them one at a time.
The back reference from OrderLine to PurchaseOrder closes the loop.
4Why It Also Loads the Whole Database
With entities loaded from the database, there is a second problem even before the stack overflows. The lines collection is lazy: Hibernate puts a wrapper there that loads the rows only when someone reads them. Lombok's toString and, for set-like collections, hashCode and equals read them, so a simple log statement runs a select for the lines. If each line has its own lazy associations, a product, a warehouse, a supplier, those are printed too and loaded too. A log line meant to show one order can walk across a large part of the object graph, one query at a time.
Outside a transaction, the same access fails differently, with a LazyInitializationException, because there is no session left to load the collection. So @Data on entities gives us three possible symptoms, a stack overflow, a flood of queries, or a lazy loading exception, depending on where the method is called.
5The Safe Approach
We keep Lombok for what it is good at and write the identity methods ourselves.
Notice what changed. @Getter and @Setter generate only accessors, which never call each other. @ToString lists only the fields we choose, and associations are never among them. equals compares the id, and treats two new objects without ids as different unless they are the same instance. hashCode returns a constant per class, so it does not change when the database assigns the id during persist, which would otherwise make the object impossible to find in a HashSet. If the entity has a natural business key that never changes, such as an order reference assigned at creation, equals and hashCode can use that instead.
Many teams go one step further and forbid @Data and @EqualsAndHashCode on entities in their code review checklist or with a static check, because the mistake is easy to make again.
6Follow-up questions
Can we fix it with @ToString.Exclude and @EqualsAndHashCode.Exclude on the associations?
That stops the recursion, and it is better than nothing. But Lombok's equals and hashCode still use all the other fields, including mutable ones such as status, so changing a field changes the hash and breaks sets. They also include the id, which changes on persist. Writing equals and hashCode by hand on the id or a business key is safer.
Does Jackson have the same problem?
Yes. Serializing a bidirectional pair to JSON follows the same loop and fails with a recursion error, and it triggers lazy loading too. The fix there is to return DTOs from controllers instead of entities, or at least to break the cycle with @JsonManagedReference and @JsonBackReference or @JsonIgnore.
Is @Data fine for DTOs and records?
Yes. A DTO has no lazy associations and usually no back references, so value-based equals and a full toString are exactly what we want. For immutable DTOs, a Java record is often even simpler than @Data.
7Conclusion
In this answer, we saw that @Data generates equals, hashCode and toString over every field, so a bidirectional @OneToMany makes them call each other until the stack overflows, and they load lazy associations along the way. The safe entity uses @Getter and @Setter, a toString with only simple fields, and hand-written equals and hashCode on the id with a constant hash, or on an immutable business key.
Open with
The cause is that @Data includes @ToString and @EqualsAndHashCode, which use every field, and Lombok does not know that some fields are JPA associations. With a bidirectional @OneToMany, the order's toString prints its lines, each line prints its order, the order prints its lines again, and so on until StackOverflowError. hashCode and equals follow the same loop, which is why adding the order to a HashSet crashes too.
Then, if they want more
With loaded entities, there is a second effect: these methods read lazy collections and associations, so a log statement can fire many selects across the graph, or throw LazyInitializationException outside a transaction.
My safe approach is to use only @Getter and @Setter on entities, a @ToString that explicitly includes simple fields like the id and reference and never associations, and to write equals and hashCode by hand. equals compares the id and returns false when the id is null, unless it is the same instance, and hashCode returns a constant per class, so the hash does not change when the id is assigned at persist.
If there is an immutable business key, I would use that instead. I would also ban @Data and @EqualsAndHashCode on entities in our review checklist.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An entity uses Lombok @Data and has a bidirectional @OneToMany. Calling toString() or...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/8ef4d958fc/