gpt4 book ai didi

java - Orika 可以映射嵌套集合吗?

转载 作者:行者123 更新时间:2023-11-29 05:13:40 28 4
gpt4 key购买 nike

我想使用 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;
}

公开课来源{ 私有(private)最终列表> list = new LinkedList>();

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

公共(public)课目的地{ 私有(private)最终列表> listDest = new LinkedList>();

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

公共(public)课主要{

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

最佳答案

你可以看到这个例子:

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);
}
}

关于java - Orika 可以映射嵌套集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382275/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com