Spring boot 使用Mapstruct将长id转换为集合列表?

Spring boot 使用Mapstruct将长id转换为集合列表?,spring-boot,mapstruct,Spring Boot,Mapstruct,在Mapstruct接口中,如何将id转换为Set 我尝试了以下操作,但失败,因为发生了错误: @Mapper(componentModel = "spring", uses = {UserSystemService.class}) public interface CompanyPostMapper extends EntityMapper<CompanyPostDTO, Company> { @Mapping(source = "userSystemId", targe

在Mapstruct接口中,如何将id转换为Set

我尝试了以下操作,但失败,因为发生了错误:

@Mapper(componentModel = "spring", uses = {UserSystemService.class})
public interface CompanyPostMapper extends EntityMapper<CompanyPostDTO, Company> {

    @Mapping(source = "userSystemId", target = "userSystems", expression = "java(userSystemService.findByIdAndAddToSet(id))")
    Company toEntity(CompanyPostDTO dto);


    default Company fromId(Long id) {
        if (id == null) {
            return null;
        }
        Company company = new Company();
        company.setId(id);
        return company;
    }
}
@Mapper(componentModel=“spring”,使用={UserSystemService.class})
公共接口CompanyPostMapper扩展了EntityMapper{
@映射(source=“userSystemId”,target=“userSystems”,expression=“java(userSystemService.findbyidanddtoset(id)))
公司对实体(公司对实体);
默认公司fromId(长id){
if(id==null){
返回null;
}
公司=新公司();
company.setId(id);
返回公司;
}
}
我不知道我是否正确理解了“uses”参数的用法,但基本上我希望获得ID并查询寄存器,然后返回一个带有寄存器的集合

我打算尝试“qualifiedByName”并在Mapper接口中创建一个方法,但我不知道如何注入存储库,也不知道这是否是一个好的实践


最好的解决方法是什么?

MapStruct是一个映射框架。您正在映射内部执行查找。当然这是可能的(看看JPA映射示例,其中使用了
@Context
)。但是您不能同时继承
EntityMapper

通常,您需要在映射逻辑之外进行查找,并使用更新方法更新对象

然后,您的映射将如下所示:

@Mapper(componentModel=“spring”)
公共接口CompanyPostMapper{
void updateEntity(CompanyPostDTO dto,@MappingTarget Company entity);
}
//你的电话看起来像:
公共类呼叫服务{
Company Company=userSystemService.findbyidanddtoset(id));
如果(公司==null){
公司=新公司();
}
公司PostMapper.updateEntity(dto,公司);
}