2016-07-14 101 views
0

我嘗試轉換DemoEntityDemoDto。他們有孩子屬性Collection<SubEntity>Collection<SubDto>。轉換Collection<SubEntity>時發生錯誤。如果我不添加@JMap("subEntities"),JMapper會運行良好。現在我希望在轉換DemoEntity時,Collection<SubEntity>可以一起轉換。JMapper子集合<Entity>轉換爲Collection <Dto>

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.subEntitiesDemoDto.subDtos
Exception in thread "main" com.googlecode.jmapper.exceptions.JMapperException: java.lang.NullPointerException 
 
\t at com.googlecode.jmapper.config.JmapperLog.ERROR(JmapperLog.java:46) 
 
\t at com.googlecode.jmapper.JMapper.<init>(JMapper.java:445) 
 
\t at com.googlecode.jmapper.JMapper.<init>(JMapper.java:411) 
 
\t at com.googlecode.jmapper.JMapper.<init>(JMapper.java:385) 
 
\t at com.jmapper.JmapperDemo.main(JmapperDemo.java:35) 
 
Caused by: java.lang.NullPointerException 
 
\t at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:107) 
 
\t at com.googlecode.jmapper.operations.complex.AComplexOperation.newInstance(AComplexOperation.java:98) 
 
\t at com.googlecode.jmapper.operations.recursive.MappedCollectionOperation.sharedCode(MappedCollectionOperation.java:64) 
 
\t at com.googlecode.jmapper.operations.complex.AComplexOperation.sourceControl(AComplexOperation.java:156) 
 
\t at com.googlecode.jmapper.operations.complex.AComplexOperation.genericFlow(AComplexOperation.java:133) 
 
\t at com.googlecode.jmapper.operations.complex.AComplexOperation.write(AComplexOperation.java:89) 
 
\t at com.googlecode.jmapper.generation.MapperConstructor.mapping(MapperConstructor.java:154) 
 
\t at com.googlecode.jmapper.generation.MapperConstructor.wrappedMapping(MapperConstructor.java:123) 
 
\t at com.googlecode.jmapper.generation.MapperConstructor.getMappings(MapperConstructor.java:89) 
 
\t at com.googlecode.jmapper.generation.MapperGenerator.generateMapperClass(MapperGenerator.java:54) 
 
\t at com.googlecode.jmapper.generation.MapperBuilder.generate(MapperBuilder.java:88) 
 
\t at com.googlecode.jmapper.JMapper.createMapper(JMapper.java:458) 
 
\t at com.googlecode.jmapper.JMapper.<init>(JMapper.java:440) 
 
\t ... 3 more

除此之外,還有什麼方法轉換到Collection<Entity>Collection<Dto>

回答

0

這一點,你需要遞歸映射的情況下,映射子類。 當您將DemoDTO與DemoEntity進行映射時,您需要將SubDTO與SubEntity進行映射。

+0

謝謝你的回答,我喜歡那樣使用。有用。但最後,我使用MapStruct,因爲它速度更快。 –

相關問題