jparepository可以在propertymap中使用吗?

ao218c7q  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(178)

我正在尝试创建一个属性Map,它允许我将简单的dtoMap到更复杂的dto。在第一个例子中,我有其他对象的id,但在第二个例子中,我需要嵌套dto。我试图创建使用服务和存储库获取所需对象并将其Map到DTO的方法(一些旧的Map器位于服务层)。不幸的是,没有调用getcitydto和getdistrictdto方法。我将非常感激你的帮助。

@Service
@RequiredArgsConstructor
public class RestAddressDtoMap extends PropertyMap<RestAddressDto, AddressDto> {

private final CityRepository cityRepository;
private final CityServiceImpl cityService;
private final DistrictRepository districtRepository;
private final DistrictServiceImpl districtService;

@Override
protected void configure() {
    map().setCity(getCityDto(source.getCityId()));
    map().setDistrict(getDistrictDto(source.getDistrictId()));
}

private CityDto getCityDto(Long id) {
    return id != null
            ? cityService.toDto(cityRepository.findOne(id))
            : null;
}

private DistrictDto getDistrictDto(Long id) {
    return id != null
            ? districtService.toDto(districtRepository.findOne(id))
            : null;
}

我也试着用转换器来Map它。

@Service
@RequiredArgsConstructor
public class RestAddressDtoMap extends PropertyMap<RestAddressDto, AddressDto> {

private final CityRepository cityRepository;
private final CityServiceImpl cityService;

@Override
protected void configure() {
    using(getCityDto).map(source.getCityId()).setCity(null);
}

private Converter<Long, CityDto> getCityDto = context ->
        (context.getSource() != null
                ? cityService.toDto(cityRepository.findOne(context.getSource()))
                : null);

我得到编译错误

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ep-rest: Compilation failure: Compilation failure: 
[ERROR] /home/.../RestAddressDtoMap.java:[48,43] variable cityService might not have been initialized

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题