1

我想使用 Orika 库映射具有嵌套集合的字段。我在课堂上的领域定义为:

private final List<List<Pojo>> list = new LinkedList<List<Pojo>>();

Pojo 是一个简单的 POJO 类。不幸的是,我在 Orika 的内部逻辑中遇到了由 NullPointerException 引起的 MappingException。

我做错了什么吗?也许我需要使用自定义映射功能?

编辑:

这是我的代码:

public class Pojo {
private int field;

public int getField() {
    return field;
}

public void setField(final int field) {
    this.field = field;
}

}

public class Source { private final List> list = new LinkedList>();

public List<List<Pojo>> getList() {
    return list;
}

}

public class Destination { private final List> listDest = new LinkedList>();

public List<List<Pojo>> getListDest() {
    return listDest;
}

}

公共类主要{

public static void main(final String[] args) {

    final MapperFactory factory = new DefaultMapperFactory.Builder().build();

    factory.classMap(Source.class, Destination.class).field("list", "listDest").byDefault().register();

    final Source src = new Source();
    final LinkedList<Pojo> nestedList = new LinkedList<Pojo>();
    final Pojo pojo = new Pojo();
    pojo.setField(8978);

    nestedList.add(pojo);

    src.getList().add(nestedList);

    final MapperFacade facade = factory.getMapperFacade();
    final Destination dest = facade.map(src, Destination.class);

    System.out.println(dest.getListDest().get(0).get(0).getField());
}

}

执行上述代码会导致此异常:

Exception in thread "main" ma.glasnost.orika.MappingException: Error encountered while mapping for the following inputs: 
rawSource=com.bbh.nested.Source@39185ce6
sourceClass=class com.bbh.nested.Source
destinationClass=class com.bbh.nested.Destination
4

2 回答 2

4

你可以看到这个例子:

public class ShopEntity {
    private Long id;
    private String name;
    private String logo;
    private String url;
    private ProductCategory mainCategory;
    private Set<ShopRel> shopRels = new HashSet<>(0);
    private Account account;
    // Assume getter/setter
}

public class ProductCategory extends BaseEntity {
    private Long id;
    private String name;
    // Assume getter/setter
}

public class ShopRel {
    private Long id;
    private SaleChannel saleChannel;
    private Boolean enabled;
    // Assume getter/setter
}

public class SaleChannel {
    private Long id;
    private String name;
    private String image;
    private String description;
    private Boolean active;
    // Assume getter/setter
}


public class ShopDto {
    private Long id;
    private String name;
    private String logo;
    private String url;
    private Long mainCategory;
    private Set<ShopRelDto> shopRelDtos = new HashSet<ShopRelDto>();
    // Assume getter/setter
}

public class ShopRelDto {
    private Long channelId;
    private String name;
    private Boolean enabled;
    // Assume getter/setter
}

public class MapperUtils {
    private static final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    private static final MapperFacade mapper = mapperFactory.getMapperFacade();
    static {
        mapperFactory.classMap(ShopEntity.class, ShopDto.class)
           .field("mainCategory.id", "mainCategory")
           .fieldMap("shopRels", "shopRelDtos").aElementType(ShopRel.class).bElementType(ShopRelDto.class).add()
           .register();
        mapperFactory.classMap(ShopRel.class, ShopRelDto.class)
           .field("saleChannel.id", "channelId")
           .field("saleChannel.name", "name")
           .field("enabled", "enabled")
           .register();
    }

    public static final void map(Object source, Object distance) {
        mapper.map(source, distance);
    }

    public static final <T> T map(Object source, Class<T> destinationClass){
        return mapper.map(source, destinationClass);
    }

    public static void main(String[] args) {
        ShopEntity shop = new ShopEntity();
        shop.setId(1L);
        shop.setName("ABC");
        ProductCategory productCategory =new ProductCategory();
        productCategory.setId(10L);
        shop.setMainCategory(productCategory);

        Set<ShopRel> shopRels = new HashSet<>(0);
        ShopSaleChannelRel channelRel = new ShopSaleChannelRel();
        channelRel.setId(1L);
        channelRel.setEnabled(true);
        SaleChannel saleChannel = new SaleChannel();
        saleChannel.setId(1L);
        saleChannel.setName("Channel1");
        channelRel.setSaleChannel(saleChannel);
        shopRels.add(channelRel);
        shop.setShopRels(shopRels);
        ShopDto shopDto = map(shop, ShopDto.class);
        System.out.println(shopDto);
    }
}
于 2017-06-02T10:31:24.410 回答
0

如果有很多这样的情况,它可能需要通过自定义进行自定义映射,您可以通过规范扩展 Orika 以支持此用例

于 2014-12-11T14:37:31.297 回答