Skip to content
Advertisement

Why does ModelMapper map collections merge-style?

In my application I am mapping data transfer objects to entities using the ModelMapper library.

One entity Library has a collection (java.util.Set) containing other entities Book. A user should be able to change the collections contents.

In this case a data transfer object UpdateDTO is used that contains the identifiers of the entities Book to be stored in the collection.

The backend maps the UpdateDTO to the entity Library using ModelMapper.

Now assume a user removing an entity Book from the collection of Library. The UpdateDTO now contains a list of all entities of Book without the ones removed by the user.

I expect the collection to be missing the entities Book removed by the user when ModelMapper maps the UpdateDTO to the entity Library. But unlike my expectation the entity returned Library still contains all Book entities.

Why is that? I have pinned down the ‘problem’ to these lines in the CollectionConverter [0].

[0] https://github.com/modelmapper/modelmapper/blob/master/core/src/main/java/org/modelmapper/internal/converter/CollectionConverter.java#L62-L63

Advertisement

Answer

The repository of ModelMapper contains an issue [0] regarding the same problem, an explanation on how the current implementation works and code so ModelMapper removes items if the source size is less than the destination size.

[0] https://github.com/modelmapper/modelmapper/issues/423

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement