Mapstruct uses not working, how to make mapstruct "uses" use other mapper for inner fields

huangapple go评论108阅读模式
英文:

Mapstruct uses not working, how to make mapstruct "uses" use other mapper for inner fields

问题

Mapstruct的"uses"选项不起作用,它将所有实体映射到一个映射器中,而不是为不同的实体使用不同的映射器。我有一个pom文件如下所示:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>${compiler.version}</version>
  5. <configuration>
  6. <source>17</source>
  7. <target>17</target>
  8. <annotationProcessorPaths>
  9. <path>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <version>${lombok.version}</version>
  13. </path>
  14. <path>
  15. <groupId>org.mapstruct</groupId>
  16. <artifactId>mapstruct-processor</artifactId>
  17. <version>${mapstruct.version}</version>
  18. </path>
  19. <path>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok-mapstruct-binding</artifactId>
  22. <version>0.2.0</version>
  23. </path>
  24. </annotationProcessorPaths>
  25. </configuration>
  26. </plugin>

RoomMapper接口如下:

  1. @Mapper(uses = {ParticipantMapper.class})
  2. public interface RoomMapper {
  3. RoomMapper INSTANCE = Mappers.getMapper(RoomMapper.class);
  4. @Mapping(target = "participant", source = "participant")
  5. RoomFullDto entityToData(RoomEntity entity);
  6. @Mapping(target = "participant", source = "participant")
  7. RoomEntity dataToEntity(RoomFullDto entity);
  8. @Named("EntityToShortData")
  9. RoomShortDto entityToShortData(RoomEntity entity);
  10. @Named("DataToEntity")
  11. RoomEntity shortDataToEntity(RoomShortDto entity);
  12. }

ParticipantMapper接口如下:

  1. @Mapper
  2. public interface ParticipantMapper {
  3. ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
  4. @Named("DataToEntity")
  5. @Mapping(target = "room.id", source = "roomId")
  6. @Mapping(target = "user.id", source = "userId")
  7. @Mapping(target = "nicknameInRoom", source = "username")
  8. ParticipantEntity dtoToEntity(ParticipantDto participantDto);
  9. @Named("EntityToData")
  10. @Mapping(target = "roomId", source = "room.id")
  11. @Mapping(target = "userId", source = "user.id")
  12. @Mapping(target = "username", source = "nicknameInRoom")
  13. ParticipantDto entityToDto(ParticipantEntity participantDto);
  14. }

在RoomMapperImpl中,Mapstruct生成的映射器未使用ParticipantMapper,因此您无法配置如何映射字段,并且忽略了某些字段。解决这个问题,您可以尝试以下几点:

  1. 确保在RoomMapper接口上使用了@Mapper(uses = {ParticipantMapper.class})

  2. 确保RoomMapperImpl类已由Mapstruct生成。您可以检查生成的源代码,看看是否有RoomMapperImpl类。

  3. 确保您的IDE或构建工具已正确配置,以确保Mapstruct的注解处理器已启用。

  4. 尝试清理和重新构建项目,以确保所有依赖项和生成的代码都处于最新状态。

如果您仍然遇到问题,可能需要检查您的依赖项版本和配置是否正确,并确保所有的注解都按照预期工作。如果问题仍然存在,请提供更多详细信息以获取更多帮助。

英文:

Mapstruct "uses" not working, it maps all entities in one mapper, instead of using different mappers for different entities
I have pom

  1. `&lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;${compiler.version}&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;source&gt;17&lt;/source&gt;
  7. &lt;target&gt;17&lt;/target&gt;
  8. &lt;annotationProcessorPaths&gt;
  9. &lt;path&gt;
  10. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  11. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  12. &lt;version&gt;${lombok.version}&lt;/version&gt;
  13. &lt;/path&gt;
  14. &lt;path&gt;
  15. &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
  16. &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
  17. &lt;version&gt;${mapstruct.version}&lt;/version&gt;
  18. &lt;/path&gt;
  19. &lt;path&gt;
  20. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  21. &lt;artifactId&gt;lombok-mapstruct-binding&lt;/artifactId&gt;
  22. &lt;version&gt;0.2.0&lt;/version&gt;
  23. &lt;/path&gt;
  24. &lt;/annotationProcessorPaths&gt;
  25. &lt;/configuration&gt;
  26. &lt;/plugin&gt;`
  1. @Mapper(uses = {ParticipantMapper.class})
  2. public interface RoomMapper {
  3. RoomMapper INSTANCE = Mappers.getMapper(RoomMapper.class);
  4. @Mapping(target = &quot;participant&quot;, source = &quot;participant&quot;)
  5. RoomFullDto entityToData(RoomEntity entity);
  6. @Mapping(target = &quot;participant&quot;, source = &quot;participant&quot;)
  7. RoomEntity dataToEntity(RoomFullDto entity);
  8. @Named(&quot;EntityToShortData&quot;)
  9. RoomShortDto entityToShortData(RoomEntity entity);
  10. @Named(&quot;DataToEntity&quot;)
  11. RoomEntity shortDataToEntity(RoomShortDto entity);
  12. }

and

  1. @Mapper
  2. public interface ParticipantMapper {
  3. ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
  4. @Named(&quot;DataToEntity&quot;)
  5. @Mapping(target = &quot;room.id&quot;, source = &quot;roomId&quot;)
  6. @Mapping(target = &quot;user.id&quot;, source = &quot;userId&quot;)
  7. @Mapping(target = &quot;nicknameInRoom&quot;, source = &quot;username&quot;)
  8. ParticipantEntity dtoToEntity (ParticipantDto participantDto);
  9. @Named(&quot;EntityToData&quot;)
  10. @Mapping(target = &quot;roomId&quot;, source = &quot;room.id&quot;)
  11. @Mapping(target = &quot;userId&quot;, source = &quot;user.id&quot;)
  12. @Mapping(target = &quot;username&quot;, source = &quot;nicknameInRoom&quot;)
  13. ParticipantDto entityToDto (ParticipantEntity participantDto);
  14. }

in RoomMapperImpl mapstruct generate without using ParticipantMapper, so i cant't configure how to map fields and it ignores some fields

  1. public RoomFullDto entityToData(RoomEntity entity) {
  2. if ( entity == null ) {
  3. return null;
  4. }
  5. RoomFullDto roomFullDto = new RoomFullDto();
  6. roomFullDto.setParticipant( participantEntitySetToParticipantDtoList( entity.getParticipant() ) );
  7. roomFullDto.setId( entity.getId() );
  8. roomFullDto.setName( entity.getName() );
  9. return roomFullDto;
  10. }
  11. protected List&lt;ParticipantDto&gt; participantEntitySetToParticipantDtoList(Set&lt;ParticipantEntity&gt; set) {
  12. if ( set == null ) {
  13. return null;
  14. }
  15. List&lt;ParticipantDto&gt; list = new ArrayList&lt;ParticipantDto&gt;( set.size() );
  16. for ( ParticipantEntity participantEntity : set ) {
  17. list.add( participantEntityToParticipantDto( participantEntity ) );
  18. }
  19. return list;
  20. }
  21. ParticipantDto participantEntityToParticipantDto(ParticipantEntity participantEntity) {
  22. if ( participantEntity == null ) {
  23. return null;
  24. }
  25. ParticipantDto participantDto = new ParticipantDto();
  26. participantDto.setId( participantEntity.getId() );
  27. participantDto.setIsBanned( participantEntity.getIsBanned() );
  28. if ( participantEntity.getRole() != null ) {
  29. participantDto.setRole( participantEntity.getRole().name() );
  30. }
  31. return participantDto;
  32. }

I tried to configure different versions of dependencies and annotationProcessors, it dont works

答案1

得分: 0

以下是翻译好的部分:

  • RoomFullDto#participant 字段类型是 java.util.List&lt;ParticipantDto&gt;(而不是 ParticipantDto
  • ParticipantEntity#participant 字段类型是 java.util.Set&lt;ParticipantEntity&gt;(而不是 ParticipantEntity
  • 我们需要声明两个不同类型之间的映射过程:Set&lt;ParticipantEntity&gt;List&lt;ParticipantDto&gt; 的转换
    1. Set&lt;ParticipantEntity&gt;List&lt;ParticipantDto&gt; 的转换
    1. List&lt;ParticipantDto&gt;Set&lt;ParticipantEntity&gt; 的转换
  • 请尝试在 ParticipantMapper.java 中添加两个额外的方法:
  • 创建一个名为 ParticipantMapper#mapToList 的新方法
  • @Mapper
  • public interface ParticipantMapper {
  • ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
  • @Named(&quot;DataToEntity&quot;)
  • @Mapping(target = &quot;room.id&quot;, source = &quot;roomId&quot;)
  • @Mapping(target = &quot;user.id&quot;, source = &quot;userId&quot;)
  • @Mapping(target = &quot;nicknameInRoom&quot;, source = &quot;username&quot;)
  • ParticipantEntity dtoToEntity (ParticipantDto participantDto);
  • @Named(&quot;EntityToData&quot;)
  • @Mapping(target = &quot;roomId&quot;, source = &quot;room.id&quot;)
  • @Mapping(target = &quot;userId&quot;, source = &quot;user.id&quot;)
  • @Mapping(target = &quot;username&quot;, source = &quot;nicknameInRoom&quot;)
  • ParticipantDto entityToDto (ParticipantEntity participantDto);
  • //+ 2 additional mapping methods without any annota
  • List&lt;ParticipantDto&gt; mapToDtoList(Set&lt;ParticipantEntity&gt; set);
  • Set&lt;ParticipantEntity&gt; mapToEntitySet(List&lt;ParticipantDto&gt; list);
  • MapStruct 文档:6. 映射集合
英文:

The RoomFullDto#participant field type is java.util.List&lt;ParticipantDto&gt; (not ParticipantDto)

The ParticipantEntity#participant field type is java.util.Set&lt;ParticipantEntity&gt; (not ParticipantEntity)

We need to declare the mapping process between two different types: Set&lt;ParticipantEntity&gt; and List&lt;ParticipantDto&gt;

  1. Set&lt;ParticipantEntity&gt; -> List&lt;ParticipantDto&gt; transformation
  2. List&lt;ParticipantDto&gt; -> Set&lt;ParticipantEntity&gt; transformation

Please try to add 2 more methods for ParticipantMapper.java:

Create a new method ParticipantMapper#mapToList:

  1. @Mapper
  2. public interface ParticipantMapper {
  3. ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
  4. @Named(&quot;DataToEntity&quot;)
  5. @Mapping(target = &quot;room.id&quot;, source = &quot;roomId&quot;)
  6. @Mapping(target = &quot;user.id&quot;, source = &quot;userId&quot;)
  7. @Mapping(target = &quot;nicknameInRoom&quot;, source = &quot;username&quot;)
  8. ParticipantEntity dtoToEntity (ParticipantDto participantDto);
  9. @Named(&quot;EntityToData&quot;)
  10. @Mapping(target = &quot;roomId&quot;, source = &quot;room.id&quot;)
  11. @Mapping(target = &quot;userId&quot;, source = &quot;user.id&quot;)
  12. @Mapping(target = &quot;username&quot;, source = &quot;nicknameInRoom&quot;)
  13. ParticipantDto entityToDto (ParticipantEntity participantDto);
  14. //+ 2 additional mapping methods without any annota
  15. List&lt;ParticipantDto&gt; mapToDtoList(Set&lt;ParticipantEntity&gt; set);
  16. Set&lt;ParticipantEntity&gt; mapToEntitySet(List&lt;ParticipantDto&gt; list);
  17. }

MapStruct Documentation: 6. Mapping collections

huangapple
  • 本文由 发表于 2023年7月23日 23:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定