Interview questions
Question 178 of 254
FreeWhat is the difference between a DTO and an Entity, and why shouldn't you always use one for the other?
Misc & Best Practices
An Entity (@Entity-annotated) is a class mapped directly to a database table, representing persistent storage structure. A DTO (Data Transfer Object) is a plain class shaped specifically for a particular use case — typically an API request/response — decoupled from the database schema. Using entities directly as API request/response objects couples your public API contract to internal database structure (a schema change breaks the API), can leak sensitive fields, and can trigger lazy-loading exceptions during serialization.
Intermediate5 min

