beginnerDTOs & Entity Mapping

Why not just return JPA `@Entity` objects directly from a REST controller?

Entities carry lazy-loaded relationships that can throw `LazyInitializationException` once serialized outside a transaction, they leak internal DB columns you may not want public (audit fields, foreign keys), and every schema change becomes an uncontrolled API contract change. A DTO is a plain object shaped exactly like the API response you intend, decoupling your database schema from your public contract.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What's a practical downside of hand-writing entity-to-DTO mapping code everywhere?← Back to all Spring Boot questions