Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自定义方法为ModelMapper定义映射_Java_Modelmapper - Fatal编程技术网

Java 使用自定义方法为ModelMapper定义映射

Java 使用自定义方法为ModelMapper定义映射,java,modelmapper,Java,Modelmapper,我想使用org.modelmapper.modelmapper将一些实体映射到另一个实体。问题是,为了给目标实体设置一些值,我需要根据源实体的值计算这个值 我制作了这样的代码: private TargerEntity convertToTargerEntity( SourceEntity src ) { this.modelMapper.typeMap( SourceEntity.class, TargerEntity.class ) .addMapping(

我想使用org.modelmapper.modelmapper将一些实体映射到另一个实体。问题是,为了给目标实体设置一些值,我需要根据源实体的值计算这个值

我制作了这样的代码:

private TargerEntity convertToTargerEntity( SourceEntity src ) {
    this.modelMapper.typeMap( SourceEntity.class, TargerEntity.class )
            .addMapping( src -> src.getUser().getId(), TargerEntity::setUserId )
            .addMapping( src -> getValueProperty(src), TargerEntity::setEvaluatedValue );

    return this.modelMapper.map( src, TargerEntity.class );
}
负责计算的方法如下所示:

private String getValueProperty( SourceEntity entity ) {
    return entity.getInformation().stream()
        .filter( property -> Objects.equals( property.getName(), "desiredPropertyValue" ) )
        .findFirst().orElse( null );
}
但是在映射时,我得到一个org.modelmapper.internal.ErrorsException,没有任何额外的消息


这种行为的原因是什么?它应该工作吗?

尝试使用
转换器

private TargerEntity convertToTargerEntity(SourceEntity src) {
    Converter<Information, String> converter =
            ctx -> ctx.getSource() == null ? "" : ctx.getSource().stream()
                    .filter(property -> Objects.equals(property.getName(), "desiredPropertyValue"))
                    .findFirst().orElse(null);

    this.modelMapper.typeMap(SourceEntity.class, TargerEntity.class 
            .addMapping(src -> src.getUser().getId(), TargerEntity::setUserId)
            .addMappings(mapper -> mapper.using(converter).map(SourceEntity::getInformation, TargerEntity::setEvaluatedValue));

    return this.modelMapper.map(src, TargerEntity.class);
}
private targetentity convertToTargetentity(SourceEntity src){
转换器=
ctx->ctx.getSource()==null?”:ctx.getSource().stream()
.filter(property->Objects.equals(property.getName(),“desiredPropertyValue”))
.findFirst().orElse(null);
this.modelMapper.typeMap(SourceEntity.class,targetentity.class
.addMapping(src->src.getUser().getId(),targetEntity::setUserId)
.addMappings(mapper->mapper.using(converter).map(SourceEntity::getInformation,TargetEntity::setEvaluatedValue));
返回这个.modelMapper.map(src,targetentity.class);
}

信息
类型替换为您的类型。转换器也可以定义为单例对象。

尝试使用
转换器

private TargerEntity convertToTargerEntity(SourceEntity src) {
    Converter<Information, String> converter =
            ctx -> ctx.getSource() == null ? "" : ctx.getSource().stream()
                    .filter(property -> Objects.equals(property.getName(), "desiredPropertyValue"))
                    .findFirst().orElse(null);

    this.modelMapper.typeMap(SourceEntity.class, TargerEntity.class 
            .addMapping(src -> src.getUser().getId(), TargerEntity::setUserId)
            .addMappings(mapper -> mapper.using(converter).map(SourceEntity::getInformation, TargerEntity::setEvaluatedValue));

    return this.modelMapper.map(src, TargerEntity.class);
}
private targetentity convertToTargetentity(SourceEntity src){
转换器=
ctx->ctx.getSource()==null?”:ctx.getSource().stream()
.filter(property->Objects.equals(property.getName(),“desiredPropertyValue”))
.findFirst().orElse(null);
this.modelMapper.typeMap(SourceEntity.class,targetentity.class
.addMapping(src->src.getUser().getId(),targetEntity::setUserId)
.addMappings(mapper->mapper.using(converter).map(SourceEntity::getInformation,TargetEntity::setEvaluatedValue));
返回这个.modelMapper.map(src,targetentity.class);
}

信息
类型替换为您的类型。转换器也可以定义为单例对象。

否,它将不起作用。如果您提供我
信息
字段结构,我将帮助映射否,它将不起作用。如果您提供我
信息
字段结构,我将帮助映射