微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可以在 PropertyMap 中使用 JpaRepository 吗?

如何解决可以在 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 举报,一经查实,本站将立刻删除。