Interview replay

Full round replay — entities at the boundary

10 minintermediate115 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

Should you return a JPA entity from a REST controller?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “Give me the strongest reason, not the first one.

    Testing: Whether they lead with coupling rather than performance.

    Scoring

    Pass: The API contract becomes the schema. Adding a column publishes it, renaming one breaks every client, and both happen in migrations that nobody reviews as API changes. The performance problems are real and secondary.

    Fail: Leads with lazy loading and stops there.

  2. 2. “Is the leaked-column problem realistic?

    Testing: Concreteness.

    Scoring

    Pass: Yes — a password hash, an internal note, a soft-delete flag, added months after the endpoint was written. No code changed, so no review would catch it; the change under review was a database migration.

    Fail: Calls it theoretical.

  3. 3. “What happens to lazy associations during serialisation?

    Testing: Both configurations.

    Scoring

    Pass: The serialiser touches them. With open-in-view on, one query per association after the transaction commits — where no timer is watching. With it off, LazyInitializationException mid-response, so the client gets a 200 and a truncated body.

    Fail: Only knows one of the two.

  4. 4. “Why do bidirectional mappings break?

    Testing: Cause, not symptom.

    Scoring

    Pass: Each side references the other, so a serialiser walking the graph never terminates — StackOverflowError, or a huge response first. The mapping is correct; walking it without knowing where to stop is not.

    Fail: Blames the mapping.

  5. 5. “So use @JsonIgnore?

    Testing: Whether they see the cost.

    Scoring

    Pass: It works and puts a serialisation concern on a persistence class so one consumer can read it. The next consumer that needs that field cannot get it. A reasonable stopgap, not the design.

    Fail: Offers it as the answer.

  6. 6. “Someone says DTOs are boilerplate. Respond.

    Testing: Arguing without being dogmatic.

    Scoring

    Pass: It is more code, and the code is the point — the response becomes something a person wrote and a reviewer can see. For read endpoints a projection or constructor query is usually less code than the entity version, because it removes the mapping too.

    Fail: Restates the principle without engaging.

  7. 7. “What about accepting an entity as a request body?

    Testing: The direction people forget.

    Scoring

    Pass: Worse. Mass assignment — a caller sets any field the entity has. Sending an id turns a create into an update against an arbitrary row, and sending a role escalates. The request DTO is the allowlist.

    Fail: Treats it as symmetric with the response case, or fine.

  8. 8. “You added a DTO and still get LazyInitializationException. Why?

    Testing: Placement.

    Scoring

    Pass: The mapping is happening after the transaction — the service returned the entity and the controller mapped it. The DTO only helps if it is built while the session is open. A transaction should end with a fully-formed value object.

    Fail: Assumes the DTO itself was wrong.

  9. 9. “Three consumers want three shapes of the same data. Now what?

    Testing: Where the entity approach fails on its own terms.

    Scoring

    Pass: Three records. One class cannot satisfy three contracts without annotations that contradict each other, and separate records mean a change for one consumer cannot break the others — which is what pays for the duplication.

    Fail: Proposes one class with conditional serialisation.

Score yourself

9/9 — you can make the coupling argument, not just the performance one 7-8 — solid; the request-body and DTO-placement answers are where points go 4-6 — you know DTOs are recommended but not what they prevent; redo the Production tier 0-3 — leak a column into a response yourself and see how invisible it is

← Back to Should you return a JPA entity from a REST controller?