The application that I am working on has numerous ...toMany relations, e.i. class Model
can have several parameters. In Unidirectional world, it is simple to manage a collection. I can clean the collection (Java List/set) and add elements again, very inefficient but for DB performance, but doable.
In a bidirectional word, mapping from DTO to an entity is a bit harder. I have to identify which elements in the collection changed, which were added and which were removed in the collection. That creates a lot of boilerplate code. Comparing IDs and filed values. If class Parameter
has another ...toMany relation, this becomes a real mess and forces to create many loops.
A few possible solutions, hoping to get more form you!
- Use model mappers such as (Dozer, MapStruct, ModelMapper, JMapper, ... you name it.) However trying ModelMapper I realized that this takes much more time than querying parameter from DB and editing only specific fields. Much more time for very complex objects.
- Track collections, write a class that keeps track of changes. However, that would require editing code in many places as well as increase collection size (any known libraries/extensions to do thatß)
- ???
What's the best approach to map DTO to entity and vice versa for a complex objects?