java mapstruct mapper clears the target collection before capturing the individual elements

When you are defining an update method with a Set you are actually telling MapStruct please update my current set with the elements from this new set. MapStruct can’t know which element of the set is the same and how to merge 2 sets.

To achieve what you are looking for you’ll have to write some custom code.

@Mapper(componentModel = "spring")
public interface RelatedCardObjectMapper {

    default Set<RelatedCardObject> updateRelatedCardObjectSetFromDto(Set<RelatedCardObjectDto> dtos, @MappingTarget Set<RelatedCardObject> entities) {
        // Somehow find the matching object from dtos to update in entities and then invoke relatedCardObjectFromDto with it


    @Mapping(source = "", target = "", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    RelatedCardObject relatedCardObjectFromDto(RelatedCardObjectDto dto, @MappingTarget RelatedCardObject entity);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top