Quick revision
The short answer
For a service with 80 DTOs, I would choose MapStruct with a project-wide policy that fails the build on unmapped fields. It generates plain, fast, readable code at compile time and reports mapping mistakes in the build, while ModelMapper guesses mappings by reflection at runtime, so its mistakes surface in production and it is slower. Hand-written mappers are perfectly good for a handful of DTOs, especially with records and static factory methods, but at 80 they become a lot of repetitive code to keep in sync.
What to remember
In this answer, we compared three ways of mapping. Hand-written code is explicit and debuggable but verbose, ModelMapper is concise but resolves mappings by reflection at runtime so mistakes appear late and it is slower, and MapStruct generates plain code at compile time with every target property checked. We also saw that mapping protects boundaries, avoiding lazy-loading problems and leaked persistence models. For 80 DTOs, MapStruct with a strict unmapped-target policy is the pragmatic choice.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For a service with 80 DTOs, I would choose MapStruct with a project-wide policy that fails the build on unmapped fields. It generates plain, fast, readable code at compile time and reports mapping mistakes in the build, while ModelMapper guesses mappings by reflection at runtime, so its mistakes surface in production and it is slower. Hand-written mappers are perfectly good for a handful of DTOs, especially with records and static factory methods, but at 80 they become a lot of repetitive code to keep in sync.
Why this question is asked
Every layered service has to copy data between its persistence model, its domain and its API. How we do that copying affects performance a little, but it affects correctness and maintenance a lot: a renamed field that is silently dropped, or a lazy JPA association loaded during JSON serialisation, are real production bugs. Interviewers ask this comparison to see whether we judge tools by when they detect errors and how visible their behaviour is, not by how few lines they need. In this answer, we will look at how each approach works, compare them on the criteria that matter, consider why mapping exists at all, and then make the choice for 80 DTOs.
1Three Ways to Map an Object
Let us take one small example and map it each way. An OrderEntity with id, customer and total must become an OrderDto with orderId, customerName and total.
Notice the last comment. ModelMapper is very concise, and it is clever: it splits names into tokens and can match customerName to customer.name. But orderId does not match id under its standard rules, so the field silently stays at its default value unless we register an explicit mapping. Nothing failed; the API simply returns the wrong data.
2How Each Approach Finds Its Mistakes
The most important difference is when an error shows up. With hand-written code, the compiler checks every accessor, but nothing tells us that a newly added DTO field was never set; for a record, though, the constructor forces us to pass every component, which is a strong guarantee. With MapStruct, the annotation processor checks every target property during the build and, with unmappedTargetPolicy set to ERROR, refuses to compile when one is missing. With ModelMapper, the mapping is worked out when the application runs. It offers a validate() method that checks registered type maps for unmapped destination properties, and we should call it in a test, but anything not covered by such a test is discovered by a user.
Performance follows the same pattern. Hand-written and MapStruct code are ordinary method calls, which the JIT compiler inlines. ModelMapper inspects classes by reflection and builds its type maps at runtime; it caches them, but each mapping still goes through more machinery. For most services the difference is small compared with a database call, but in a hot path mapping thousands of objects per request it becomes visible.
| Criterion | Hand-written | MapStruct | ModelMapper |
|---|---|---|---|
| Mistakes found | compile time, partly | compile time, all targets | runtime, or in tests with validate() |
| Speed | plain calls | plain generated calls | reflection, slower |
| Visibility of logic | in our code | generated source we can read | hidden in conventions |
| Code to write for 80 DTOs | a lot | small interfaces | least |
| Debugging | easy | easy, breakpoints in generated class | hard, inside the library |
| Build setup | none | annotation processor | runtime dependency |
3Why We Map at All: The Design Side
Before choosing a tool, it is worth remembering why mapping exists. Returning JPA entities directly from a controller is tempting, but it causes two kinds of trouble. First, lazy loading: an entity's associations are loaded on first access, so when Jackson serialises the entity it may trigger extra queries, or throw a LazyInitializationException once the persistence context is closed. Second, coupling: every column we add becomes part of the public API, and every API change forces a schema change.
A DTO, a data transfer object, is a simple type designed for one boundary. Mapping into it makes us decide exactly which data leaves the service, and the mapping runs inside the transaction, where lazy associations can be loaded deliberately or fetched with a join. So mapping is not ceremony; it protects the boundary. That also means we should not create DTOs where there is no boundary to protect, a point that matters when the count reaches 80.
4Making the Choice for 80 DTOs
With 80 DTOs, hand-written mapping means many hundreds of lines of code whose only job is to copy fields, and a reviewer has to check each one. That cost grows with every change to an entity. ModelMapper removes the typing but moves the checking to runtime, which is exactly the wrong direction for code with this many fields. MapStruct sits in the right place: we write only the differences, the processor writes the rest, and the build checks everything.
So I would choose MapStruct, with three rules. Set unmappedTargetPolicy to ERROR for the whole project through a shared @MapperConfig, so a forgotten field fails the build. Keep complex logic, such as currency conversion or permission-based fields, in named Java methods that the mapper calls, rather than in expression strings. And test the few mappings with real logic directly; the purely mechanical ones are already checked by the compiler.
Hand-written mappers still have a place. For a DTO with unusual logic, a static factory method on a record can be clearer than a mapper full of annotations, and mixing the two in one project is fine.
5Follow-up questions
Is ModelMapper ever the right choice?
It can suit prototypes, internal tools or a small number of structurally identical types, where speed of writing matters more than safety. If it is used, every type map should be validated in a test with validate(). For a long-lived service with many DTOs, the runtime discovery of mistakes is a poor trade.
Does MapStruct help with the lazy-loading problem?
Indirectly. It runs where we call it, usually inside the service method and its transaction, so lazy associations are accessed while the session is open. But if a mapper walks a collection that was not fetched, it will still trigger one query per entity, so we still design fetch plans, for example with a join fetch or an entity graph, for what the DTO needs.
What about records with static factory methods instead of MapStruct?
They are an excellent hand-written option: the canonical constructor forces every component to be supplied, so a forgotten field is a compile error too. For ten DTOs that may be all we need. At 80, MapStruct saves a great deal of repetitive code with the same compile-time safety.
6Conclusion
In this answer, we compared three ways of mapping. Hand-written code is explicit and debuggable but verbose, ModelMapper is concise but resolves mappings by reflection at runtime so mistakes appear late and it is slower, and MapStruct generates plain code at compile time with every target property checked. We also saw that mapping protects boundaries, avoiding lazy-loading problems and leaked persistence models. For 80 DTOs, MapStruct with a strict unmapped-target policy is the pragmatic choice.
Open with
For 80 DTOs I would pick MapStruct. The main criterion for me is when mistakes are found. MapStruct is an annotation processor, so it generates plain Java mapping code at compile time and checks every target property; with unmappedTargetPolicy set to ERROR in a shared MapperConfig, a forgotten field fails the build.
Then, if they want more
The generated code is ordinary accessor and constructor calls, so it is fast and I can read and debug it. ModelMapper is the opposite: it matches properties by naming conventions using reflection at runtime.
It is very concise, but if a name does not match, like orderId and id, the field just stays empty and I find out in production unless I validate every type map in tests. It is also slower because of the reflection.
Hand-written mappers are explicit and safe, especially static factory methods on records, where the constructor forces every field, and for a few DTOs that is what I would do. At 80 it is a lot of repetitive code to review and keep in sync.
I would also remember why we map: DTOs keep JPA entities out of the API, which avoids lazy-loading surprises during serialisation and stops the schema from becoming the public contract. So I would only create DTOs where a boundary needs them.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare MapStruct with ModelMapper and hand-written mapping code for a service with 80...": https://scrivio-interview-library.netlify.app/java/utility-libraries/71997dd4ef/