可以在 PropertyMap 中使用 JpaRepository 吗?

问题描述

我正在尝试创建 PropertyMap,它允许我将简单的 DTO 映射到更复杂的 DTO。在第一个有其他对象的 id 但在第二个我需要嵌套 DTO。我试图创建使用服务和存储库来获取需要的对象并将它们映射到 DTO(一些旧的映射器位于服务层)的方法。不幸的是,没有调用方法 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;
}

我也尝试过使用转换器映射它。

@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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...