20

I am using following mapper to map entities:

public interface AssigmentFileMapper {

AssigmentFileDTO assigmentFileToAssigmentFileDTO(AssigmentFile assigmentFile);

AssigmentFile assigmentFileDTOToAssigmentFile(AssigmentFileDTO assigmentFileDTO);

@Mapping(target = "data", ignore = true)
List<AssigmentFileDTO> assigmentFilesToAssigmentFileDTOs(List<AssigmentFile> assigmentFiles);

List<AssigmentFile> assigmentFileDTOsToAssigmentFiles(List<AssigmentFileDTO> assigmentFileDTOs);
}

I need to ignore the "data" field only for entities that mapped as collection. But it looks like @Mapping works only for single entities. Also I've noticed that generated method assigmentFilesToAssigmentFileDTOs just uses assigmentFileToAssigmentFileDTO in for-loop. Is there any solution for that?

Dmitry K
  • 1,142
  • 2
  • 16
  • 27

1 Answers1

56

MapStruct uses the assignment that it can find for the collection mapping. In order to achieve what you want you will have to define a custom method where you are going to ignore the data field explicitly and then use @IterableMapping(qualifiedBy) or @IterableMapping(qualifiedByName) to select the required method.

Your mapper should look like:

public interface AssigmentFileMapper {

    AssigmentFileDTO assigmentFileToAssigmentFileDTO(AssigmentFile assigmentFile);

    AssigmentFile assigmentFileDTOToAssigmentFile(AssigmentFileDTO assigmentFileDTO);

    @IterableMapping(qualifiedByName="mapWithoutData")
    List<AssigmentFileDTO> assigmentFilesToAssigmentFileDTOs(List<AssigmentFile> assigmentFiles);

    List<AssigmentFile> assigmentFileDTOsToAssigmentFiles(List<AssigmentFileDTO> assigmentFileDTOs);

    @Named("mapWithoutData")
    @Mapping(target = "data", ignore = true)
    AssignmentFileDto mapWithouData(AssignmentFile source)

}

You should use org.mapstruct.Named and not javax.inject.Named for this to work. You can also define your own annotation by using org.mapstruct.Qualifier

You can find more information here in the documentation.

Filip
  • 19,269
  • 7
  • 51
  • 60
  • @Fillip I have the same the problem. I did what you mentioned above but its not working at all. The impl generated is exactly what is expected with properties excluded in the entity list to dto list mapping. But dont know why its getting executed. – ajm Nov 11 '19 at 17:06
  • I don't quite follow what problem you are facing. You should provide some examples of what you've tried and wasn't working – Filip Nov 11 '19 at 17:42