Quick revision
The short answer
The standard allows @Getter, @Setter only where a field may really change, and @NoArgsConstructor(access = PROTECTED) on entities, and forbids @Data, @EqualsAndHashCode or @ToString without exclusions, and @AllArgsConstructor exposing every field. Equality is written by hand on the id or a natural key and covered by tests, associations never appear in toString, equals or hashCode, and read models and API responses use records or separate DTOs instead of entities. It is enforced with lombok.config flagUsage settings and an ArchUnit or static analysis rule that checks every class annotated @Entity.
What to remember
In this answer, we wrote a persistence standard for Lombok on entities: @Getter, selective @Setter and a protected no-args constructor are allowed, while @Data, unfiltered @ToString and @EqualsAndHashCode, and public all-args constructors are forbidden. Equality is hand-written on the id or a natural key and tested, associations stay out of toString, equals and hashCode, and records or DTOs serve read models and APIs. The rules are enforced with lombok.config flags and ArchUnit checks on every @Entity class, with the reason for each rule written next to it.
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.
The standard allows @Getter, @Setter only where a field may really change, and @NoArgsConstructor(access = PROTECTED) on entities, and forbids @Data, @EqualsAndHashCode or @ToString without exclusions, and @AllArgsConstructor exposing every field. Equality is written by hand on the id or a natural key and covered by tests, associations never appear in toString, equals or hashCode, and read models and API responses use records or separate DTOs instead of entities. It is enforced with lombok.config flagUsage settings and an ArchUnit or static analysis rule that checks every class annotated @Entity.
Why this question is asked
With 40 teams, entity bugs repeat: every few weeks someone meets StackOverflowError in a log line, a LazyInitializationException from toString, or a Set that loses entities after save. A persistence coding standard prevents the whole class of bugs at once, but only if it is short, explains its reasons and is enforced by tools. Interviewers ask this design question to see whether we can write such a standard and make it stick. In this answer, we will set out the Lombok rules for entities and the reason for each, show a model entity, cover equality and associations, explain where records and DTOs fit, and describe how the standard is enforced.
1Why Entities Need Their Own Rules
An entity is not an ordinary Java object. Hibernate creates it with a no-args constructor, may replace it with a proxy subclass, fills its fields from the database, tracks its changes, and loads its associations lazily. Lombok's general-purpose generated methods assume plain objects: equals and hashCode over all fields, toString over all fields, constructors over all fields. Each of those assumptions is wrong for entities, which is why the standard has to be explicit about what is allowed.
| Annotation | Rule | Reason |
|---|---|---|
@Getter | allowed | no hidden behaviour |
@Setter | only on fields that may change | id and associations are managed, not set freely |
@NoArgsConstructor(access = PROTECTED) | required | JPA needs it; protected discourages misuse |
@ToString | only with associations excluded | recursion and lazy loading |
@EqualsAndHashCode | forbidden; hand-write equality | all-field hash breaks after persist, proxies |
@Data | forbidden | bundles all of the above problems |
@AllArgsConstructor (public) | forbidden | bypasses invariants; field order is fragile |
@Builder | only on a validating constructor | builders make every field optional |
2A Model Entity
The standard includes a complete example that teams can copy. It shows the allowed annotations, the protected constructor, a public constructor that takes the mandatory fields, setters only where change is legitimate, and the hand-written equality.
Notice that toString lists only what is explicitly included, which keeps associations and sensitive columns out by default. The Hibernate.getClass call unwraps proxies; it may initialise a proxy, so teams that compare many lazy references can use the unwrapping shown with HibernateProxy instead. Check the helper methods your Hibernate version offers.
3Equality and Associations
Equality gets its own section because it causes the subtlest bugs. The rule: equals compares the id only when it is not null, or an immutable natural key if the entity has one, and hashCode is constant per class or based on that natural key, so it never changes when the id is assigned. Every entity has a short unit test for this: two new instances are not equal, an entity is still found in a HashSet after persist, and a proxy obtained with getReferenceById() equals the loaded entity.
Associations are never part of toString, equals or hashCode. Including them causes infinite recursion in bidirectional relationships, triggers lazy loading or LazyInitializationException, and makes equality depend on the state of other objects. The standard also asks for helper methods that keep both sides of a bidirectional association in step, such as order.addLine(line), instead of setters on the collection.
- Has an immutable natural key?assigned at creation, never changesYesequals and hashCode on the keyNoUse the generated id
- Use the generated idequals on id only when not null
- hashCodeconstant per class, proxies unwrapped
- Add the equality testnew instances, Set after persist, proxy vs loaded
4 steps in order. Play them, or step through them one at a time.
4Records and DTOs for Read Models and APIs
Many entity problems come from using entities where they do not belong: returned from controllers, serialised to JSON, cached, or passed across layers. Serialising an entity walks its associations, which brings back lazy loading and recursion, and couples the API to the table structure. The standard therefore says that API responses and read models use separate types, records being the natural choice on current Java. Spring Data JPA can project query results straight into records, which also loads only the columns needed.
Entities stay inside the transactional service layer, and mapping to records happens there, so lazy associations are never touched by a JSON serialiser.
5Enforcing the Standard
Forty teams will not follow a document on their own, so the standard is backed by tools. A shared lombok.config, shipped with the company build, sets lombok.data.flagUsage = error for persistence modules, or warning while existing code is cleaned up, so @Data fails the build. An ArchUnit test, distributed in a shared test library, checks every class annotated @Entity: no @Data, no @EqualsAndHashCode, no @AllArgsConstructor, a protected or public no-args constructor present, and equals and hashCode declared in the class itself.
Notice the comment: most Lombok annotations have source retention and are gone from the compiled classes, so a bytecode tool such as ArchUnit cannot see @Data itself. It can see the result, though, and the @lombok.Generated marker on generated methods, which is why the rule checks that each entity has equals and hashCode that were not generated, while the lombok.config flags catch the annotations at compile time. The review checklist and a short training session with the bugs behind each rule complete the picture, and the rules are reviewed once a year.
6Follow-up questions
Why forbid a public all-args constructor on entities?
It lets callers set the id, the version and associations directly, bypassing the invariants the mandatory-field constructor enforces, and its parameter order follows the field order, so reordering fields silently changes the meaning of calls. A constructor with only the mandatory fields states what an entity needs to exist.
Is @Setter on every field really a problem?
Yes for the id, the version field and collections, which Hibernate manages; replacing a mapped collection with a setter is a known source of orphan-removal errors. Setters on ordinary columns that genuinely change are fine.
How do you handle the existing code?
We start with warnings, fix the entities that have caused incidents first, and make the rules errors module by module. The ArchUnit test can use a frozen baseline of existing violations, so new code must comply while old code is fixed over time.
7Conclusion
In this answer, we wrote a persistence standard for Lombok on entities: @Getter, selective @Setter and a protected no-args constructor are allowed, while @Data, unfiltered @ToString and @EqualsAndHashCode, and public all-args constructors are forbidden. Equality is hand-written on the id or a natural key and tested, associations stay out of toString, equals and hashCode, and records or DTOs serve read models and APIs. The rules are enforced with lombok.config flags and ArchUnit checks on every @Entity class, with the reason for each rule written next to it.
Open with
The standard starts from the fact that entities are not plain objects: Hibernate creates them through a no-args constructor, swaps in proxy subclasses, loads associations lazily and assigns ids at persist.
Then, if they want more
So for Lombok it says: allowed are @Getter, @Setter only on fields that may really change, never on the id or collections, and @NoArgsConstructor with protected access, which JPA needs. Forbidden are @Data, @EqualsAndHashCode, @ToString without exclusions and public @AllArgsConstructor, and @Builder only on a validating constructor.
Each rule comes with the bug it prevents: recursion and lazy loading from toString, Sets breaking because the hash changes at persist, proxies comparing unequal.
Equality is written by hand: equals compares the id only when it is not null, after unwrapping proxies, or an immutable natural key, and hashCode is constant per class, with a unit test for each entity. Associations never appear in toString, equals or hashCode.
Read models and API responses use records or DTOs, for example Spring Data projections into records, so entities are never serialised.
Enforcement: a shared lombok.config with data flagUsage set to error in persistence modules, and an ArchUnit rule in a shared test library that checks every @Entity class has hand-written equals and hashCode, since @Data itself is not in the bytecode. Existing code gets a frozen baseline and is fixed module by module.
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 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 "You are writing the persistence coding standard for 40 teams using Spring Data JPA. What...": https://scrivio-interview-library.netlify.app/java/lombok/e7f628982a/