JMapper 将子 Collection<Entity> 转换为 Collection<Dto>
JMapper convert child Collection<Entity> to Collection<Dto>
我正在尝试将 DemoEntity 转换为 DemoDto。他们有子属性 Collection<SubEntity>
和 Collection<SubDto>
。转换 Collection<SubEntity>
时出错。如果我不添加 @JMap("subEntities")
,JMapper 将运行良好。现在我希望在转换DemoEntity
时,Collection<SubEntity>
可以一起转换。
public class DemoEntity {
/** The id. */
private int id;
/** The code. */
private String code;
/** The date. */
private Date date;
private Collection<SubEntity> subEntities;
@JMapConversion(from = "date", to = "date")
public String convertDate(Date date) {
return date.toString();
}
public DemoEntity(int id, String code, Date date, Collection<SubEntity> subEntities) {
super();
this.id = id;
this.code = code;
this.date = date;
this.subEntities = subEntities;
}
...setter and getter...
}
public class DemoDto {
/** The id. */
@JMap
private Long id;
/** The code. */
@JMap
private String code;
/** The date. */
@JMap
private String date;
@JMap("subEntities")
private Collection<SubDto> subDtos;
... setter and getter...
}
主要方法如下:
Collection<SubEntity> subEntities = new ArrayList<>();
for (int i = 1; i < 100; i++) {
SubEntity subEntity = new SubEntity(i, "C" + i, new Date());
subEntities.add(subEntity);
}
DemoEntity demoEntity = new DemoEntity(1, "D" + 1, new Date(), subEntities);
JMapperAPI api = new JMapperAPI();
MappedClass mappedClass = new MappedClass(SubDto.class);
api.add(mappedClass);
JMapper<DemoDto, DemoEntity> mapper = new JMapper<>(DemoDto.class, DemoEntity.class, api);
DemoDto demoDto = mapper.getDestination(demoEntity);
System.out.println(demoDto);
当隐蔽 DemoEntity.subEntities 到 DemoDto.subDtos 时发生异常
Exception in thread "main" com.googlecode.jmapper.exceptions.JMapperException: java.lang.NullPointerException
at com.googlecode.jmapper.config.JmapperLog.ERROR(JmapperLog.java:46)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:445)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:411)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:385)
at com.jmapper.JmapperDemo.main(JmapperDemo.java:35)
Caused by: java.lang.NullPointerException
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:107)
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:98)
at com.googlecode.jmapper.operations.recursive.MappedCollectionOperation.sharedCode(MappedCollectionOperation.java:64)
at com.googlecode.jmapper.operations.complex.AComplexOperation.sourceControl(AComplexOperation.java:156)
at com.googlecode.jmapper.operations.complex.AComplexOperation.genericFlow(AComplexOperation.java:133)
at com.googlecode.jmapper.operations.complex.AComplexOperation.write(AComplexOperation.java:89)
at com.googlecode.jmapper.generation.MapperConstructor.mapping(MapperConstructor.java:154)
at com.googlecode.jmapper.generation.MapperConstructor.wrappedMapping(MapperConstructor.java:123)
at com.googlecode.jmapper.generation.MapperConstructor.getMappings(MapperConstructor.java:89)
at com.googlecode.jmapper.generation.MapperGenerator.generateMapperClass(MapperGenerator.java:54)
at com.googlecode.jmapper.generation.MapperBuilder.generate(MapperBuilder.java:88)
at com.googlecode.jmapper.JMapper.createMapper(JMapper.java:458)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:440)
... 3 more
此外,有什么方法可以将Collection<Entity>
转换为Collection<Dto>
这是递归映射的情况,需要映射子类。
当您将 DemoDTO 映射到 DemoEntity 时,您需要将 SubDTO 映射到 SubEntity。
我正在尝试将 DemoEntity 转换为 DemoDto。他们有子属性 Collection<SubEntity>
和 Collection<SubDto>
。转换 Collection<SubEntity>
时出错。如果我不添加 @JMap("subEntities")
,JMapper 将运行良好。现在我希望在转换DemoEntity
时,Collection<SubEntity>
可以一起转换。
public class DemoEntity {
/** The id. */
private int id;
/** The code. */
private String code;
/** The date. */
private Date date;
private Collection<SubEntity> subEntities;
@JMapConversion(from = "date", to = "date")
public String convertDate(Date date) {
return date.toString();
}
public DemoEntity(int id, String code, Date date, Collection<SubEntity> subEntities) {
super();
this.id = id;
this.code = code;
this.date = date;
this.subEntities = subEntities;
}
...setter and getter...
}
public class DemoDto {
/** The id. */
@JMap
private Long id;
/** The code. */
@JMap
private String code;
/** The date. */
@JMap
private String date;
@JMap("subEntities")
private Collection<SubDto> subDtos;
... setter and getter...
}
主要方法如下:
Collection<SubEntity> subEntities = new ArrayList<>();
for (int i = 1; i < 100; i++) {
SubEntity subEntity = new SubEntity(i, "C" + i, new Date());
subEntities.add(subEntity);
}
DemoEntity demoEntity = new DemoEntity(1, "D" + 1, new Date(), subEntities);
JMapperAPI api = new JMapperAPI();
MappedClass mappedClass = new MappedClass(SubDto.class);
api.add(mappedClass);
JMapper<DemoDto, DemoEntity> mapper = new JMapper<>(DemoDto.class, DemoEntity.class, api);
DemoDto demoDto = mapper.getDestination(demoEntity);
System.out.println(demoDto);
Exception in thread "main" com.googlecode.jmapper.exceptions.JMapperException: java.lang.NullPointerException
at com.googlecode.jmapper.config.JmapperLog.ERROR(JmapperLog.java:46)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:445)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:411)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:385)
at com.jmapper.JmapperDemo.main(JmapperDemo.java:35)
Caused by: java.lang.NullPointerException
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:107)
at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:98)
at com.googlecode.jmapper.operations.recursive.MappedCollectionOperation.sharedCode(MappedCollectionOperation.java:64)
at com.googlecode.jmapper.operations.complex.AComplexOperation.sourceControl(AComplexOperation.java:156)
at com.googlecode.jmapper.operations.complex.AComplexOperation.genericFlow(AComplexOperation.java:133)
at com.googlecode.jmapper.operations.complex.AComplexOperation.write(AComplexOperation.java:89)
at com.googlecode.jmapper.generation.MapperConstructor.mapping(MapperConstructor.java:154)
at com.googlecode.jmapper.generation.MapperConstructor.wrappedMapping(MapperConstructor.java:123)
at com.googlecode.jmapper.generation.MapperConstructor.getMappings(MapperConstructor.java:89)
at com.googlecode.jmapper.generation.MapperGenerator.generateMapperClass(MapperGenerator.java:54)
at com.googlecode.jmapper.generation.MapperBuilder.generate(MapperBuilder.java:88)
at com.googlecode.jmapper.JMapper.createMapper(JMapper.java:458)
at com.googlecode.jmapper.JMapper.<init>(JMapper.java:440)
... 3 more
此外,有什么方法可以将Collection<Entity>
转换为Collection<Dto>
这是递归映射的情况,需要映射子类。 当您将 DemoDTO 映射到 DemoEntity 时,您需要将 SubDTO 映射到 SubEntity。