错误尝试在映射中转换对象

发布于 2025-02-08 06:35:24 字数 721 浏览 3 评论 0 原文

我正在尝试将classDomain类转换为classentity,但是它不起作用,因为它在下面返回错误,我不知道该如何解决:

error: The return type List<DomainClass> is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.

类dataDomainClass:

public class DataDomainClass{
    private List<DomainClass> data;
    }

class entityClass:

public class EntityClass {

    private String codDist;

    private String numF;

    private String textJus;

    private Boolean valResol;
    }

convert mapper:

DataDomainClass map(EntityClass entity);

domainClass类包含与相同的字段EntityClass类。

提前致谢。

I'm trying to convert a ClassDomain class into a ClassEntity but it's not working because it's returning the error below and I don't know how to solve it:

error: The return type List<DomainClass> is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.

Class DataDomainClass:

public class DataDomainClass{
    private List<DomainClass> data;
    }

Class EntityClass:

public class EntityClass {

    private String codDist;

    private String numF;

    private String textJus;

    private Boolean valResol;
    }

Convert mapper:

DataDomainClass map(EntityClass entity);

The DomainClass class contains the same fields as the EntityClass class.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩套路吗 2025-02-15 06:35:24

没有明确的脱机实现。映射不支持映射从不可学到到峰值。 。这是针对原语的更通用的解决方案。

对于您的情况,最简单的解决方案是:

解决方案1:映射实体的自定义方法

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Mapper
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    @Mapping(target = "data", source = "entity")
    DataDomainClass map(EntityClass entity);

    DomainClass toDomain(EntityClass entity);

    default List<DomainClass> toList(EntityClass entity) {
        return entity != null ? Collections.singletonList(toDomain(entity)) :
                                new ArrayList<>();
    }
}

生成的代码:

public class DomainMapperImpl implements DomainMapper {

    @Override
    public DataDomainClass map(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DataDomainClass dataDomainClass = new DataDomainClass();
        dataDomainClass.data = toList( entity );
        return dataDomainClass;
    }

    @Override
    public DomainClass toDomain(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DomainClass domainClass = new DomainClass();
        domainClass.codDist = entity.codDist;
        domainClass.numF = entity.numF;
        domainClass.textJus = entity.textJus;
        domainClass.valResol = entity.valResol;
        return domainClass;
    }
}

解决方案2:表达式

import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.Collections;

@Mapper(imports = {Collections.class})
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    @Mapping(target = "data", expression = "java(Collections.singletonList(toDomain(entity)))")
    DataDomainClass map(EntityClass entity);

    DomainClass toDomain(EntityClass entity);
}

生成的代码:

public class DomainMapperImpl implements DomainMapper {

    @Override
    public DataDomainClass map(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DataDomainClass dataDomainClass = new DataDomainClass();
        dataDomainClass.data = Collections.singletonList(toDomain(entity));
        return dataDomainClass;
    }

    @Override
    public DomainClass toDomain(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DomainClass domainClass = new DomainClass();
        domainClass.codDist = entity.codDist;
        domainClass.numF = entity.numF;
        domainClass.textJus = entity.textJus;
        domainClass.valResol = entity.valResol;
        return domainClass;
    }
}

更新:

也不支持非意识映射。在 iToser-to-non-titerable

案例反向映射的示例:

@Mapper
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    default EntityClass map(DataDomainClass dataDomainClass) {        
        return dataDomainClass != null && dataDomainClass.data != null && !dataDomainClass.data.isEmpty() ? toEntity(dataDomainClass.data.get(0)) : null;
    }

    EntityClass toEntity(DomainClass domainClass);
}

There is no clear out-of-box implementation. MapStruct does not support mapping from non-iterable to iterable. Workaround described in mapstruct-examples. This is more general solution for primitives.

For your case simplest solutions are:

Solution 1: custom method for mapping entity to List

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Mapper
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    @Mapping(target = "data", source = "entity")
    DataDomainClass map(EntityClass entity);

    DomainClass toDomain(EntityClass entity);

    default List<DomainClass> toList(EntityClass entity) {
        return entity != null ? Collections.singletonList(toDomain(entity)) :
                                new ArrayList<>();
    }
}

Generated code:

public class DomainMapperImpl implements DomainMapper {

    @Override
    public DataDomainClass map(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DataDomainClass dataDomainClass = new DataDomainClass();
        dataDomainClass.data = toList( entity );
        return dataDomainClass;
    }

    @Override
    public DomainClass toDomain(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DomainClass domainClass = new DomainClass();
        domainClass.codDist = entity.codDist;
        domainClass.numF = entity.numF;
        domainClass.textJus = entity.textJus;
        domainClass.valResol = entity.valResol;
        return domainClass;
    }
}

Solution 2: Expression

import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.Collections;

@Mapper(imports = {Collections.class})
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    @Mapping(target = "data", expression = "java(Collections.singletonList(toDomain(entity)))")
    DataDomainClass map(EntityClass entity);

    DomainClass toDomain(EntityClass entity);
}

Generated code:

public class DomainMapperImpl implements DomainMapper {

    @Override
    public DataDomainClass map(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DataDomainClass dataDomainClass = new DataDomainClass();
        dataDomainClass.data = Collections.singletonList(toDomain(entity));
        return dataDomainClass;
    }

    @Override
    public DomainClass toDomain(EntityClass entity) {
        if ( entity == null ) {
            return null;
        }
        DomainClass domainClass = new DomainClass();
        domainClass.codDist = entity.codDist;
        domainClass.numF = entity.numF;
        domainClass.textJus = entity.textJus;
        domainClass.valResol = entity.valResol;
        return domainClass;
    }
}

UPDATE:

Iterable to non-iterable mapping also is not supported. Workaround for primitive types described at iterable-to-non-iterable.

Example of reverse mapping for your case:

@Mapper
public interface DomainMapper {
    DomainMapper INSTANCE = Mappers.getMapper(DomainMapper.class);

    default EntityClass map(DataDomainClass dataDomainClass) {        
        return dataDomainClass != null && dataDomainClass.data != null && !dataDomainClass.data.isEmpty() ? toEntity(dataDomainClass.data.get(0)) : null;
    }

    EntityClass toEntity(DomainClass domainClass);
}

╰◇生如夏花灿烂 2025-02-15 06:35:24

映射无法将非意素类型映射到迭代中,尤其是嵌套集合不可绘制的映射。

以下是几种解决方案:

  1. 更改方法签名,因此输入和输出参数将是相同的类型(iTable或nor-Ableable),并在代码中进一步进行包装。这是最可取的解决方案

  2. ,您也可以在接口中创建默认方法以在此处进行包装:

      @mapper
    公共接口entity2DomainMapper {
    
        DomainClass Entity2Domain(EntityClass RAW);
    
        (EntityClass RAW){
            domainClass nested = Entity2Domain(raw);
            返回datadomainclass.of(list.of(nested));
        }
    }
     

    因此,您没有调用自动生成的方法,而是调用默认方法

  3. ,或者如果您想将所有包装隐藏在Mapsstruct Mapper中,则可以使用@后图,但它仍然需要您自己实现包裹

@Mapper
public abstract class Entity2DomainMapper2 {
    abstract DomainClass entity2Domain(EntityClass raw);

    abstract DataDomainClass entity2DataDomain(EntityClass raw);

    @AfterMapping
    protected void afterMapping(EntityClass raw, @MappingTarget DataDomainClass target) {
        if (target.getNested() == null) {
            target.setNested(new ArrayList<>());
        }
        DomainClass nested = entity2Domain(raw);
        target.getNested().add(nested);
    }
}

ept:您也可以使用 Extensions 如Eugene所指定的,但是最清洁的方法是使映射器尽可能简单(理想情况下,根本不应该有非自动化代码),并处理填充 list&lt; domainClass&gt; 在代码中的其他地方。

MapStruct can't map non-iterable type to iterable, especially mapping non-iterable to a nested collection.

Here's several solutions:

  1. Change method signatures so input and output parameters will be the same type(iterable or non-iterable) and do the wrapping further in code. This is the most preferable solution

  2. Also you can create default method in your interface to do the wrapping there:

    @Mapper
    public interface Entity2DomainMapper {
    
        DomainClass entity2Domain(EntityClass raw);
    
        default DataDomainClass from(EntityClass raw) {
            DomainClass nested = entity2Domain(raw);
            return DataDomainClass.of(List.of(nested));
        }
    }
    

    So instead of calling auto-generated method you'll call default method

  3. Or if you wanna hide all the wrapping inside MapStruct mapper you can use @AfterMapping, but it still will require you to implement the wrapping by yourself

@Mapper
public abstract class Entity2DomainMapper2 {
    abstract DomainClass entity2Domain(EntityClass raw);

    abstract DataDomainClass entity2DataDomain(EntityClass raw);

    @AfterMapping
    protected void afterMapping(EntityClass raw, @MappingTarget DataDomainClass target) {
        if (target.getNested() == null) {
            target.setNested(new ArrayList<>());
        }
        DomainClass nested = entity2Domain(raw);
        target.getNested().add(nested);
    }
}

UPD: You can also use extensions as Eugene specified, but the cleanest approach will be to keep mapper as simple as possible(ideally there should be no non-autogenerated code at all) and handle populating List<DomainClass> somewhere else in code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文