英文:
Mapstruct uses not working, how to make mapstruct "uses" use other mapper for inner fields
问题
Mapstruct的"uses"选项不起作用,它将所有实体映射到一个映射器中,而不是为不同的实体使用不同的映射器。我有一个pom文件如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
RoomMapper接口如下:
@Mapper(uses = {ParticipantMapper.class})
public interface RoomMapper {
RoomMapper INSTANCE = Mappers.getMapper(RoomMapper.class);
@Mapping(target = "participant", source = "participant")
RoomFullDto entityToData(RoomEntity entity);
@Mapping(target = "participant", source = "participant")
RoomEntity dataToEntity(RoomFullDto entity);
@Named("EntityToShortData")
RoomShortDto entityToShortData(RoomEntity entity);
@Named("DataToEntity")
RoomEntity shortDataToEntity(RoomShortDto entity);
}
ParticipantMapper接口如下:
@Mapper
public interface ParticipantMapper {
ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
@Named("DataToEntity")
@Mapping(target = "room.id", source = "roomId")
@Mapping(target = "user.id", source = "userId")
@Mapping(target = "nicknameInRoom", source = "username")
ParticipantEntity dtoToEntity(ParticipantDto participantDto);
@Named("EntityToData")
@Mapping(target = "roomId", source = "room.id")
@Mapping(target = "userId", source = "user.id")
@Mapping(target = "username", source = "nicknameInRoom")
ParticipantDto entityToDto(ParticipantEntity participantDto);
}
在RoomMapperImpl中,Mapstruct生成的映射器未使用ParticipantMapper,因此您无法配置如何映射字段,并且忽略了某些字段。解决这个问题,您可以尝试以下几点:
-
确保在RoomMapper接口上使用了
@Mapper(uses = {ParticipantMapper.class})
。 -
确保RoomMapperImpl类已由Mapstruct生成。您可以检查生成的源代码,看看是否有RoomMapperImpl类。
-
确保您的IDE或构建工具已正确配置,以确保Mapstruct的注解处理器已启用。
-
尝试清理和重新构建项目,以确保所有依赖项和生成的代码都处于最新状态。
如果您仍然遇到问题,可能需要检查您的依赖项版本和配置是否正确,并确保所有的注解都按照预期工作。如果问题仍然存在,请提供更多详细信息以获取更多帮助。
英文:
Mapstruct "uses" not working, it maps all entities in one mapper, instead of using different mappers for different entities
I have pom
`<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>`
@Mapper(uses = {ParticipantMapper.class})
public interface RoomMapper {
RoomMapper INSTANCE = Mappers.getMapper(RoomMapper.class);
@Mapping(target = "participant", source = "participant")
RoomFullDto entityToData(RoomEntity entity);
@Mapping(target = "participant", source = "participant")
RoomEntity dataToEntity(RoomFullDto entity);
@Named("EntityToShortData")
RoomShortDto entityToShortData(RoomEntity entity);
@Named("DataToEntity")
RoomEntity shortDataToEntity(RoomShortDto entity);
}
and
@Mapper
public interface ParticipantMapper {
ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
@Named("DataToEntity")
@Mapping(target = "room.id", source = "roomId")
@Mapping(target = "user.id", source = "userId")
@Mapping(target = "nicknameInRoom", source = "username")
ParticipantEntity dtoToEntity (ParticipantDto participantDto);
@Named("EntityToData")
@Mapping(target = "roomId", source = "room.id")
@Mapping(target = "userId", source = "user.id")
@Mapping(target = "username", source = "nicknameInRoom")
ParticipantDto entityToDto (ParticipantEntity participantDto);
}
in RoomMapperImpl mapstruct generate without using ParticipantMapper, so i cant't configure how to map fields and it ignores some fields
public RoomFullDto entityToData(RoomEntity entity) {
if ( entity == null ) {
return null;
}
RoomFullDto roomFullDto = new RoomFullDto();
roomFullDto.setParticipant( participantEntitySetToParticipantDtoList( entity.getParticipant() ) );
roomFullDto.setId( entity.getId() );
roomFullDto.setName( entity.getName() );
return roomFullDto;
}
protected List<ParticipantDto> participantEntitySetToParticipantDtoList(Set<ParticipantEntity> set) {
if ( set == null ) {
return null;
}
List<ParticipantDto> list = new ArrayList<ParticipantDto>( set.size() );
for ( ParticipantEntity participantEntity : set ) {
list.add( participantEntityToParticipantDto( participantEntity ) );
}
return list;
}
ParticipantDto participantEntityToParticipantDto(ParticipantEntity participantEntity) {
if ( participantEntity == null ) {
return null;
}
ParticipantDto participantDto = new ParticipantDto();
participantDto.setId( participantEntity.getId() );
participantDto.setIsBanned( participantEntity.getIsBanned() );
if ( participantEntity.getRole() != null ) {
participantDto.setRole( participantEntity.getRole().name() );
}
return participantDto;
}
I tried to configure different versions of dependencies and annotationProcessors, it dont works
答案1
得分: 0
以下是翻译好的部分:
RoomFullDto#participant
字段类型是java.util.List<ParticipantDto>
(而不是ParticipantDto
)ParticipantEntity#participant
字段类型是java.util.Set<ParticipantEntity>
(而不是ParticipantEntity
)- 我们需要声明两个不同类型之间的映射过程:
Set<ParticipantEntity>
和List<ParticipantDto>
的转换 -
Set<ParticipantEntity>
到List<ParticipantDto>
的转换
-
List<ParticipantDto>
到Set<ParticipantEntity>
的转换
- 请尝试在
ParticipantMapper.java
中添加两个额外的方法: - 创建一个名为
ParticipantMapper#mapToList
的新方法 @Mapper
public interface ParticipantMapper {
ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
@Named("DataToEntity")
@Mapping(target = "room.id", source = "roomId")
@Mapping(target = "user.id", source = "userId")
@Mapping(target = "nicknameInRoom", source = "username")
ParticipantEntity dtoToEntity (ParticipantDto participantDto);
@Named("EntityToData")
@Mapping(target = "roomId", source = "room.id")
@Mapping(target = "userId", source = "user.id")
@Mapping(target = "username", source = "nicknameInRoom")
ParticipantDto entityToDto (ParticipantEntity participantDto);
//+ 2 additional mapping methods without any annota
List<ParticipantDto> mapToDtoList(Set<ParticipantEntity> set);
Set<ParticipantEntity> mapToEntitySet(List<ParticipantDto> list);
- MapStruct 文档:6. 映射集合
英文:
The RoomFullDto#participant
field type is java.util.List<ParticipantDto>
(not ParticipantDto
)
The ParticipantEntity#participant
field type is java.util.Set<ParticipantEntity>
(not ParticipantEntity
)
We need to declare the mapping process between two different types: Set<ParticipantEntity>
and List<ParticipantDto>
Set<ParticipantEntity>
->List<ParticipantDto>
transformationList<ParticipantDto>
->Set<ParticipantEntity>
transformation
Please try to add 2 more methods for ParticipantMapper.java:
Create a new method ParticipantMapper#mapToList:
@Mapper
public interface ParticipantMapper {
ParticipantMapper INSTANCE = Mappers.getMapper(ParticipantMapper.class);
@Named("DataToEntity")
@Mapping(target = "room.id", source = "roomId")
@Mapping(target = "user.id", source = "userId")
@Mapping(target = "nicknameInRoom", source = "username")
ParticipantEntity dtoToEntity (ParticipantDto participantDto);
@Named("EntityToData")
@Mapping(target = "roomId", source = "room.id")
@Mapping(target = "userId", source = "user.id")
@Mapping(target = "username", source = "nicknameInRoom")
ParticipantDto entityToDto (ParticipantEntity participantDto);
//+ 2 additional mapping methods without any annota
List<ParticipantDto> mapToDtoList(Set<ParticipantEntity> set);
Set<ParticipantEntity> mapToEntitySet(List<ParticipantDto> list);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论