Quick revision
The short answer
I would allow a short list of Lombok annotations whose meaning is clear and narrow, @Getter, @RequiredArgsConstructor for constructor injection, @Builder and @Value for immutable types, and @Slf4j, and discourage @Data, especially on JPA entities and on classes that hold injected collaborators. For new immutable DTOs on Java 17 and later, records come first. The rules live in a central lombok.config (flagging @Data, marking generated code for coverage tools) and are enforced continuously through static analysis and review checklists, not by a one-time cleanup.
What to remember
In this answer, we saw that @Data is harmful on entities and on beans with collaborators because of its generated equals, hashCode, toString and setters. The guideline allows a small set of clear annotations, @Getter, @RequiredArgsConstructor, @Builder, @Value and @Slf4j, prefers records for new immutable DTOs, configures lombok.config centrally to flag @Data and mark generated code, and is enforced continuously by static analysis and review checklists, starting with the classes where bugs actually happen.
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.
I would allow a short list of Lombok annotations whose meaning is clear and narrow, @Getter, @RequiredArgsConstructor for constructor injection, @Builder and @Value for immutable types, and @Slf4j, and discourage @Data, especially on JPA entities and on classes that hold injected collaborators. For new immutable DTOs on Java 17 and later, records come first. The rules live in a central lombok.config (flagging @Data, marking generated code for coverage tools) and are enforced continuously through static analysis and review checklists, not by a one-time cleanup.
Why this question is asked
A report that 60 percent of classes use @Data tells us that @Data has become the default way to write a class, whatever the class is for. That is a problem because @Data makes equality, mutability and logging decisions that are right for some classes and wrong for most. Interviewers ask this leadership question to see whether we can turn technical knowledge into guidelines that a large organisation will actually follow. In this answer, we will look at what @Data does to each kind of class in the report, build the allowed list and the reasons for it, set up lombok.config centrally, and plan how to roll it out and enforce it.
1What @Data Does to Each Kind of Class
@Data combines getters, setters, a required-args constructor, toString, and equals and hashCode over all fields. For a mutable DTO that only carries data to a JSON response, that is mostly harmless. For other classes it causes real bugs.
On JPA entities, the generated equals and hashCode use all fields, including the generated id and associations, so the hash changes when the entity is saved and Sets of entities break; toString walks lazy associations, which triggers database queries, LazyInitializationException or StackOverflowError on bidirectional relations. On services with injected dependencies, @Data generates setters that let anyone replace a collaborator, a toString that prints repositories and clients (and can call into Spring proxies), and an equals that compares services by their dependencies, which is meaningless.
| Kind of class | What @Data adds that hurts | What to use instead |
|---|---|---|
| JPA entity | all-field equals, hashCode, toString over associations | @Getter, chosen @Setter, hand-written equals |
| Service with dependencies | setters on collaborators, toString of beans | @RequiredArgsConstructor, final fields |
| Immutable DTO | setters and mutable equality | record, or @Value |
| Mutable request DTO | little; acceptable | @Getter @Setter, or record |
| Configuration properties | setters needed for binding | @ConfigurationProperties record |
@Data fits only a small part of the report.2The Allowed List and the Reasons
The guideline should be short enough to remember. I would allow freely: @Getter, because a getter has no hidden behaviour; @RequiredArgsConstructor, the standard way to write constructor injection for Spring beans with final fields; @Builder and @Value for immutable types where a builder helps readability; and @Slf4j for loggers. @Setter is allowed on chosen fields, and @ToString and @EqualsAndHashCode only with explicit includes or excludes, so the equality and the logged fields are visible in the source.
@Data is discouraged everywhere and forbidden on entities and on Spring beans. For new immutable DTOs, records come first on Java 17 and later, because they need no tool, show their components in the declaration, and are understood by Jackson and Spring's configuration binding. Experimental annotations, and annotations such as @SneakyThrows that change Java's rules, are not allowed in shared code.
One detail for constructor injection: if a constructor parameter needs @Qualifier, Lombok must be told to copy that annotation from the field, with lombok.copyableAnnotations in lombok.config. That belongs in the central config so every team gets it.
3A Central lombok.config
Lombok reads lombok.config files from the source tree, and a file at the repository root applies to every module. We ship it through the project template or the shared build, so the settings are the same everywhere.
Notice that @Data is flagged as a warning rather than an error at first. With 60 percent of classes using it, an error would break every build overnight. Once a module is cleaned up, it can raise the setting to error in its own lombok.config.
4Rolling It Out and Enforcing It
A one-time cleanup does not hold; within a year the numbers creep back. So I would combine guidance and continuous checks. The guideline goes into the engineering handbook with the reasons and short examples like the ones above. A review checklist item asks: does this class need generated equals and toString, and on which fields? Static analysis rules, for example a source-level rule in the existing analyser (most Lombok annotations are gone from the bytecode, so bytecode tools such as ArchUnit see only their effects), fail the build when @Data, @EqualsAndHashCode or @ToString without excludes appear on classes annotated @Entity, or when a Spring bean has @Setter.
For the existing code, I would fix the dangerous cases first, entities and beans, because those cause real bugs, and leave harmless @Data on simple DTOs to be replaced when teams touch those files. I would track the numbers in the same report that started this: @Data on entities and beans should drop to zero; on DTOs it can fall gradually as records replace them.
- Publish the guidelineallowed list, reasons, examples
- Ship central lombok.configflag
@Dataas warning, mark generated code - Add analysis rulesno
@Dataor unfiltered equals on@Entityand beans - Fix entities and beans firstthe classes where bugs happen
- Replace DTO @Data on contactrecords for new code
- Track the numberssame report, every quarter
6 steps in order. Play them, or step through them one at a time.
5Follow-up questions
Why not just ban Lombok?
Because some of it is genuinely useful and low risk. @RequiredArgsConstructor and @Slf4j remove noise without hiding decisions. The problem in the report is one annotation used as a default, not the tool as a whole. Removing Lombok entirely is a much bigger and separate decision.
Why mark generated code with @lombok.Generated?
Coverage tools like JaCoCo skip methods carrying an annotation named Generated, so generated getters and equals no longer inflate or deflate coverage. The coverage numbers then reflect code people actually wrote.
How do you handle teams that disagree?
I explain the bugs behind each rule, with examples from our own incidents if we have them, and keep an exception path for genuine cases. Rules that come with reasons and real failures are followed far more readily than bare bans.
6Conclusion
In this answer, we saw that @Data is harmful on entities and on beans with collaborators because of its generated equals, hashCode, toString and setters. The guideline allows a small set of clear annotations, @Getter, @RequiredArgsConstructor, @Builder, @Value and @Slf4j, prefers records for new immutable DTOs, configures lombok.config centrally to flag @Data and mark generated code, and is enforced continuously by static analysis and review checklists, starting with the classes where bugs actually happen.
Open with
First I would look at what @Data actually does to the classes in that report. On a simple DTO it is mostly harmless. On JPA entities its all-field equals and hashCode break Sets when the id is generated, and its toString walks lazy associations, causing queries, LazyInitializationException or StackOverflowError.
Then, if they want more
On services it adds setters for injected collaborators and a toString and equals over beans, which makes no sense.
So my guideline is a short allowed list: @Getter, @RequiredArgsConstructor for constructor injection with final fields, @Builder and @Value for immutable types, and @Slf4j. @Setter on chosen fields, and @ToString or @EqualsAndHashCode only with explicit includes or excludes. @Data is discouraged and forbidden on entities and beans, and new immutable DTOs should be records on Java 17 and later.
I would ship a central lombok.config: lombok.addLombokGeneratedAnnotation = true so coverage tools skip generated code, lombok.data.flagUsage = warning so every @Data is visible, experimental features flagged as errors, and copyableAnnotations for @Qualifier.
Then enforcement that lasts: a static analysis rule that fails builds with @Data on @Entity classes or Spring beans, a review checklist item, and the same report tracked every quarter. I would fix entities and beans first, because that is where the bugs are, and let DTOs change as teams touch them.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "A static analysis report shows that 60 percent of your company's Java classes use @Data...": https://scrivio-interview-library.netlify.app/java/lombok/b34dd031eb/