Quick revision
The short answer
I would move business rules into the entities as methods that change several fields consistently, such as order.cancel(reason), remove the public setters, expose collections as unmodifiable views with methods that keep both sides of an association in sync, and keep separate DTOs at the API edge. JPA only needs a no-argument constructor, which can be protected, and with field access it never calls setters; Jackson can bind DTOs through constructors or records. I would make the change gradually, behind tests, starting with the entities that cause the most bugs.
What to remember
In this answer, we took an anaemic JPA model with public setters and rules scattered across services and moved the rules into intention-revealing entity methods such as cancel, which update several fields consistently. We saw that JPA only needs a protected no-argument constructor and, with field access, never calls setters, that Jackson can bind request records through constructors, and that DTOs keep the API edge separate. We exposed collections as unmodifiable views and kept both sides of a bidirectional association in sync, and planned a gradual migration behind tests, starting with the buggiest entities.
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 move business rules into the entities as methods that change several fields consistently, such as order.cancel(reason), remove the public setters, expose collections as unmodifiable views with methods that keep both sides of an association in sync, and keep separate DTOs at the API edge. JPA only needs a no-argument constructor, which can be protected, and with field access it never calls setters; Jackson can bind DTOs through constructors or records. I would make the change gradually, behind tests, starting with the entities that cause the most bugs.
Why this question is asked
Many Spring services end up with an anaemic domain model: entities that are bags of public getters and setters, and service classes full of if statements that decide what a valid order or a valid cancellation looks like. The rules are scattered, duplicated and easy to bypass, because any code that holds an entity can set any field. Teams often keep it that way because they believe JPA and Jackson need setters. Interviewers ask this question to see whether we know what those frameworks really require and whether we can plan a safe change in a live code base. In this answer, we will look at the problem, move a rule into the entity, check JPA's and Jackson's real constraints, fix associations, and plan the migration.
1A Quick Recap of the Problem
A JPA entity is a class mapped to a database table, managed by an EntityManager, the object that loads entities, tracks changes and writes them back at commit. When every field of an Order entity has a public setter, any service, controller or mapper can change any field at any time. The rule 'a shipped order cannot be cancelled, and cancelling records a reason and a time' then lives in whichever service method remembers it.
Notice that the second service broke three rules in one line, and nothing stopped it. The entity has no way to defend itself because its setters accept anything. This is the scattered-rules problem: every new caller is a new chance to forget a step.
2Moving the Rule Into the Entity
The fix is to give the entity a method for the business operation, and let that method change all the related fields together. The setters for those fields disappear.
Now every path that cancels an order goes through the same three checks and the same three field updates. The service method becomes a thin coordinator: it opens the transaction, loads the entity and delegates. Notice that we do not call save; inside a transaction, Hibernate's dirty checking, its comparison of the entity with the state it loaded, writes the changed columns at commit. Passing a Clock instead of calling Instant.now() makes the method easy to test with a fixed time.
3What JPA and Hibernate Really Require
The usual objection is that JPA needs setters. It does not. The JPA specification requires a no-argument constructor that is public or protected, so a protected constructor that only JPA uses is enough, and the class must not be final if we want Hibernate to create lazy-loading proxies, subclasses it generates at runtime. Beyond that, what matters is the access type.
JPA decides the access type from where we put @Id. If @Id is on a field, as above, the provider uses field access: it reads and writes the fields directly through reflection and never calls getters or setters. Only with property access, @Id on a getter, does it use getter and setter pairs. So with field access we are free to have no setters at all and to name our read methods however we like.
| Framework | Belief | What it actually needs |
|---|---|---|
| JPA / Hibernate | public setters | no-arg constructor, may be protected; field access via @Id on field |
| Hibernate proxies | nothing special | non-final class and methods for lazy proxies |
| Jackson | default constructor plus setters | constructor, @JsonCreator, record, or field access |
| Bean Validation | getters | annotations on fields work |
4Keeping the API Edge Separate
The other source of setters is JSON binding: controllers often accept and return entities directly, so Jackson sets fields from the request. That couples the public API to the table layout and lets a client send fields it should never control, such as status. The cleaner design is a DTO, a data transfer object, at the edge. Jackson binds records through their canonical constructor, so a request DTO needs no setters either.
The client can now send only a reason. It cannot set a status, a timestamp or an id, because those fields do not exist in the DTO. The entity is free to change its internal structure without breaking the API, and the API can evolve without touching the table.
5Associations: Unmodifiable Views and Both Sides in Sync
Collections are where entities leak most. A bidirectional association, where Order has a list of OrderLine and each OrderLine points back to its Order, must be updated on both sides, or the database row and the in-memory objects disagree. With a public getter on the list, callers add lines without setting the back reference.
The owning side, the side whose foreign key column is written, is OrderLine here, because mappedBy marks Order's list as the inverse side. If code only added to the list, Hibernate would not write the foreign key at all. The addLine method makes it impossible to get this wrong, and the unmodifiable view closes the back door. Because we use field access, Hibernate still manages the real list internally.
6Introducing the Change Gradually
A big-bang rewrite of every entity would be risky and hard to review. I would first look at the bug history and pick the two or three entities with the most defects, often orders, payments or subscriptions. For each one, I would write tests around current behaviour, add the new business methods, move callers to them one service at a time, and only then delete the setters, letting the compiler list any remaining callers. Each step is a small pull request that can ship on its own.
7Follow-up questions
Do we lose anything by removing setters from entities?
Some convenience in tests and mappers, which used setters to build objects quickly. We replace that with constructors or factory methods that create valid objects, and with test data builders. Mapping libraries such as MapStruct can map through constructors and builders. The gain is that an invalid entity becomes hard to create anywhere, including in tests.
Should entities contain all business logic then?
No. Rules about one entity's own state belong in the entity, such as cancel or addLine. Rules that involve several aggregates, external services or repositories, such as checking stock in another service before confirming, belong in an application service or a domain service. The goal is that each rule lives in one place, closest to the data it protects.
8Conclusion
In this answer, we took an anaemic JPA model with public setters and rules scattered across services and moved the rules into intention-revealing entity methods such as cancel, which update several fields consistently. We saw that JPA only needs a protected no-argument constructor and, with field access, never calls setters, that Jackson can bind request records through constructors, and that DTOs keep the API edge separate. We exposed collections as unmodifiable views and kept both sides of a bidirectional association in sync, and planned a gradual migration behind tests, starting with the buggiest entities.
Open with
I would move the business rules into the entities as methods named after operations, like order.cancel with a reason. That method checks that the order is not shipped, then sets the status, the reason and the timestamp together, so no caller can do half of it.
Then, if they want more
The services become thin: open a transaction, load, delegate, and let dirty checking write the changes. Then I remove the public setters. People often think JPA needs them, but it does not.
JPA needs a no-argument constructor, which can be protected, and if @Id is on a field it uses field access and reads and writes fields directly, never calling setters. For Hibernate's lazy proxies the class should not be final.
At the API edge, I would stop binding entities from JSON and use request and response DTOs; Jackson can bind records through their constructors, so DTOs do not need setters either, and clients can no longer send fields like status.
For collections, I would return an unmodifiable view and add methods like addLine that set both sides of a bidirectional association, so the owning side is always correct.
Finally I would do this gradually: start with the entities that have the most bugs, put tests around current behaviour, move callers one at a time, and delete each setter when the compiler shows no more users.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "JPA entities in your service have public setters on every field, and business rules are...": https://scrivio-interview-library.netlify.app/java/oop-principles/69f5a6fb90/