PO, DTO, VO: When Splitting Java Layers Pays Off and When It's Just Ceremony
Mass assignment attacks and leaked fields are real production incidents, not theoretical concerns. A clear split rule, enforced at compile time with tools like MapStruct and ArchUnit, protects interfaces from database changes and keeps sensitive data inside the service layer without over-engineering.
Returning a JPA entity or MyBatis PO straight from a Controller leaks password hashes, phone numbers, and logical delete flags, and leaves write endpoints open to mass assignment attacks. This piece lays out the concrete costs of skipping the separation: sensitive field exposure, lazy-loading crashes, bidirectional serialization stack overflows, contract breakage when DB columns change, and wasted mobile bandwidth.
The decision framework is scenario-driven, not dogmatic: split when data crosses process or team boundaries, when interface fields differ from table fields, when change rhythms differ, or when security/serialization requirements differ. Small teams and internal tools can skip or simplify; external APIs, multi-end apps, and microservices must split. A middle path keeps PO independent while merging DTO and VO for simple interfaces.
For the conversion layer, MapStruct wins over BeanUtils because it generates compile-time code, fails the build on unmapped fields, and runs ~80x faster than reflection-based copying. Advanced patterns include CQRS-style SQL projections that output VOs directly, annotation-driven masking via Jackson serializers, ArchUnit rules that block PO leakage in CI, and validation groups or records for clean DTOs.
Change frequency is the real driver: PO changes with DB schema, DTO with interface contracts, VO with UI updates. When those rhythms diverge, merging the objects couples unrelated teams.
The strongest argument for splitting is security, not architecture. A whitelist VO is a syntactic guarantee that sensitive fields cannot leak, which review processes alone rarely enforce.
Rejecting BeanUtils is not just about performance; reflection-based copying hides mapping errors until runtime, turning refactoring into a debugging session.
The article's pragmatic stance, allowing simple projects to skip splitting, is more useful than dogmatic layering advice. The decision tree based on boundary crossing and change rhythm gives teams a measurable rule.
CI-enforced rules like ArchUnit shift the burden from human memory to automated gates, which is the only way to keep architecture rules alive in a fast-moving team.
The discussion splits between those who see the PO/DTO/VO split as essential for safety and contractual clarity and those who find it divorced from real-world chaos where developers collapse everything into Maps or plain XML. Several practices surface: VO for output, Entity mapped to the table, Form for input/query, DTO as an anti-corruption layer for third-party calls. The thread also raises the shift in habits under AI-era tooling, and one reply notes that standards only work when the team respects role boundaries, not naming conventions.
The theory is nice, but in practice it's just all-in one go.
Yes, exactly. [crying emoji]
What a waste of ambition. Three years ago, when I first started working, I pursued standard, standardized development, worked hard to learn all these different entity classes (VO, QO, DTO), MapStruct... Now that the AI era is here, programming rules have changed too.
Learned something new; it's still useful. Only knowing the 'what' lets you understand the 'why'.
At my previous company, the tech lead was young and liked standardized code, so everything was split into DO, DTO, VO, and Query classes. At my current company, it's a bunch of old-timers. The whole project uses Maps for everything. The API has to be documented. The only DO and VO I wrote were by me. The classic thing is everything is in XML, nobody knows how to use MyBatis-Plus.
There's a saying: 'To blend with the world's dust'—yeah, that fits.