Quick revision
The short answer
Using entities as API responses, events and reports has tied our database schema to every consumer, so renaming a column or splitting a table became a breaking change for teams we do not control, and it spread over-fetching and lazy loading problems into controllers, serialisers and event publishers. The policy I would set is that entities stay inside the service layer, APIs and events use explicit, versioned contracts, reads use projections, SQL-first tools are allowed for read-heavy parts, and we apply it whenever code changes rather than through a big rewrite.
What to remember
In this answer, we looked at what years of using entities as API responses, events and reports have cost: a database schema coupled to every consumer, leaked fields, and lazy loading and over-fetching spread through every layer. We set a policy that keeps entities inside the service layer, gives APIs and events explicit contracts, serves reads with projections and allows SQL-first tools for read-heavy work, and we roll it out on change with contract tests and an architecture check rather than a rewrite.
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.
Using entities as API responses, events and reports has tied our database schema to every consumer, so renaming a column or splitting a table became a breaking change for teams we do not control, and it spread over-fetching and lazy loading problems into controllers, serialisers and event publishers. The policy I would set is that entities stay inside the service layer, APIs and events use explicit, versioned contracts, reads use projections, SQL-first tools are allowed for read-heavy parts, and we apply it whenever code changes rather than through a big rewrite.
Why this question is asked
Returning an entity from a controller is the fastest way to ship the first version of an endpoint, and for years nobody notices the cost. Then a schema change breaks a mobile app, an event payload grows to 200 KB because a collection got serialised, or a report times out because it loaded a graph of 30 entities per row. Interviewers ask a twenty-year engineer this question to see whether they can name a systemic cost, explain it to the business and set a policy that fixes it without stopping product work. In this answer, we will look at what the coupling really costs, how the performance problems spread, the policy and the reasoning behind each rule, and how to roll it out on change.
1How the Shortcut Started
An entity is a class that mirrors a table: its fields are columns, its associations are foreign keys. When a controller returns it, Jackson serialises every field, so the JSON shape is whatever the table looks like today. When an event publisher sends it, the event schema is the table too. And when a report reads entities, it gets the table's shape and the ORM's loading behaviour.
Each of these choices saved a day at the time. Together, they mean that the database schema, the most internal detail of a service, has become a public contract consumed by web clients, partner integrations, other services and the data warehouse.
Notice what leaks: an internal score nobody meant to publish, a legacy column, and a collection that may trigger a lazy load or an exception depending on settings.
2What the Coupling Has Cost
The first cost is that schema changes became breaking changes. Splitting the name column into first and last name, or moving an address into its own table, changes the JSON of every API and event that exposed the entity. So teams stop changing the schema, and the data model freezes around decisions made years ago. Or they change it and a consumer breaks in production, which is how most of us discovered the problem.
The second cost is that we cannot tell who depends on what. With an explicit contract, a field exists because someone added it to the contract. With entities, every field is published, so any field may be in use, and removing one requires asking every consumer.
The third cost is security and privacy. Every new column is published by default, including internal flags and personal data, unless someone remembers to add @JsonIgnore.
3How Performance Problems Spread to Every Layer
When an entity travels outside the service, lazy associations travel with it. Serialising an order touches its customer, its lines and each line's product, each a separate query if lazy, or a LazyInitializationException if the session is closed. Teams then turn on open session in view or make associations eager to stop the errors, which loads more data everywhere, including in places that do not need it.
Reports and events suffer the same way: an event built from an entity carries the whole graph, and a report that iterates entities runs N+1 queries for every page. The fixes end up scattered across controllers, mappers and serialisation annotations instead of living in one query.
| Use | Cost we see today |
|---|---|
| API response | schema changes break clients; internal fields leak |
| Event payload | consumers bound to table layout; large payloads |
| Report | N+1 queries, full graphs loaded per row |
| Controller and view | OSIV or EAGER added to stop lazy errors |
4The Policy and the Reasoning Behind It
The policy has four rules. Entities stay inside the service layer of the service that owns the table; nothing outside a transaction boundary holds one. APIs and events use explicit contracts, records or schema-defined types (OpenAPI, Avro or JSON Schema), versioned and reviewed, owned by the team that publishes them. Reads use projections shaped for the use case, so a list endpoint selects the columns it shows and nothing else. And read-heavy parts such as reports, exports and search may use SQL-first tools like JdbcClient, jOOQ or database views, because there explicit SQL is clearer than an ORM.
The reasoning is to separate things that change for different reasons. The schema changes when the domain model or performance needs change. An API changes when its consumers need something. An event changes on a negotiated timeline with its subscribers. Giving each its own type means each can change without breaking the others, and the mapping between them is the one place where we decide how an internal change looks from outside.
- API and event contractsrecords, OpenAPI or Avro schemas; versioned; public
- Service layercommands load entities; reads return projections
- Entities and repositoriesprivate to the owning service
- SQL-first read layer
JdbcClient, jOOQ, views for reports and exports - Database schemafree to change behind the layers above
5 layers, from the top down. Step through them one at a time.
Notice the last comment. The schema changed, one query changed, and no consumer noticed. That is the whole argument in one example, and it is the one I would show to leadership.
5Applying It on Change, Not as a Rewrite
Rewriting hundreds of endpoints to DTOs would take a year and deliver nothing the business can see, so it would be stopped halfway. Instead, the policy applies on change: any endpoint or event that is touched for a feature gets an explicit contract as part of that work, and every new endpoint starts with one. We freeze the current JSON shape of each touched endpoint in a contract test before changing it, so the move to an explicit type does not change the payload by accident.
We also pick a few high-value targets on purpose: the endpoints and events with the most consumers, the ones blocking a planned schema change, and the reports with the worst query counts. A lint rule or ArchUnit test that fails when a controller returns an @Entity type stops new violations, and a dashboard of remaining violations shows progress. In my experience, about 80 percent of traffic flows through 20 percent of endpoints, so the change pays off long before it is complete.
6Follow-up questions
Isn't mapping between entities and DTOs a lot of boilerplate?
Less than it was. Records make DTOs one line, projections let the query fill them directly, and MapStruct generates mappers where we do need to map. The boilerplate is the price of being able to change the schema, and it is far cheaper than coordinating a breaking change with ten consumers.
How do you handle events that consumers already depend on?
Publish a new versioned event type with an explicit schema alongside the old one, give consumers a migration window, measure who still consumes the old one, and then retire it. A schema registry with compatibility checks prevents the problem from coming back.
What do you tell a team that says entities in APIs are fine for internal services?
Internal consumers break just as easily, and they are harder to find because nobody signed a contract. For a small service with one consumer it can be a reasonable shortcut, but the policy should make it a conscious exception, not the default.
7Conclusion
In this answer, we looked at what years of using entities as API responses, events and reports have cost: a database schema coupled to every consumer, leaked fields, and lazy loading and over-fetching spread through every layer. We set a policy that keeps entities inside the service layer, gives APIs and events explicit contracts, serves reads with projections and allows SQL-first tools for read-heavy work, and we roll it out on change with contract tests and an architecture check rather than a rewrite.
Open with
The main cost has been coupling. When entities are the API response, the event payload and the report row, the database schema is a public contract, so renaming a column or splitting a table breaks consumers we may not even know about.
Then, if they want more
In practice teams either stop evolving the schema or break someone in production. It also leaks fields by default, including internal and personal data.
The second cost is performance spreading everywhere: entities carry lazy associations into controllers, serialisers and event publishers, so we get N+1 queries, LazyInitializationExceptions, and then workarounds like open session in view or eager fetching that load more data for everyone.
The policy I would set is: entities never leave the service layer of the owning service; APIs and events use explicit versioned contracts, records or schema-defined types; reads use projections shaped for the use case; and read-heavy parts like reports may use SQL-first tools such as JdbcClient, jOOQ or views. I would not do a big rewrite.
The rule applies whenever an endpoint or event is touched, with a contract test freezing the current payload first, plus an ArchUnit check so no new controller returns an entity, and a few high-value targets picked on purpose. That way the cost is spread over normal work and the benefit arrives early.
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 "Over many years, your services have used JPA entities as the shape of API responses...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/9bc794e52d/