Why should a JPA Entity not be the same object you return as a REST DTO?
PICTURE THIS: HOW TO EXPLAIN IT
Simple meaning
Entities are tied to the persistence context, lazy proxies, and often more fields than the API should expose.
WHY — JPA instead of guessing?
Why interviewers care about JPA:
on JPA.
the situation, the default choice, and one exception - that reads as experience.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1Entities are tied to
the persistence context, lazy proxies, and often more fields than the API should expose.
- 2Returning them leaks internals
and can trigger lazy loads after the session closes.
- 3Map to a DTO
or a projection so the JSON contract stays stable.
- 4Give an example
One tiny concrete case you can say aloud.
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
Entities are tied to the persistence context, lazy proxies, and often more fields than the API should expose. Returning them leaks internals and can trigger lazy loads after the session closes.