所以我在Orika 1.4.6和1.5.1中尝试过这个。我试图将包含Set的对象映射到包含Map的对象。这些类看起来如下......
public class Apple {
private Long id;
private String name;
private Set<Seed> seeds;
}
public class AppleDto {
private Long id;
private String name;
private Map<Long, SeedDto> seeds = new HashMap<Long, SeedDto>();
}
public class Seed {
private Long id;
private String name;
}
public class SeedDto {
private Long id;
private String name;
}
我觉得我正在按用户指南(enter link description here:在“映射嵌套的多次出现元素”下)中的定义正确定义映射...)
factory.classMap(Apple.class, AppleDto.class)
.field("id", "id")
.field("name", "name")
.field("seeds{id}", "seeds{key}")
.field("seeds{}", "seeds{value}")
.register();
factory.classMap(Seed.class, SeedDto.class)
.field("id", "id")
.field("name", "name")
.register();
然而,当我尝试进行映射时,我收到以下错误......
ma.glasnost.orika.MappingException: While attempting the following mapping:
sourceClass = class testpackage.Apple
destinationType = testpackage.AppleDto
resolvedStrategy = InstantiateAndUseCustomMapperStrategy<Apple, AppleDto> {customMapper: GeneratedMapper<Apple, AppleDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<Seed, MapEntry<Long, SeedDto>>, DefaultBoundMapperFacade<Seed, SeedDto>, DefaultBoundMapperFacade<Entry<Long, SeedDto>, Seed>], usedTypes: [Set<Seed>, ArrayList<MapEntry<Long, SeedDto>>, Map<Long, SeedDto>] }, unenhancer: ma.glasnost.orika.unenhance.BaseUnenhancer@5e0e82ae, objectFactory: DefaultConstructorObjectFactory<AppleDto>}
Error occurred: java.lang.ClassCastException: java.lang.Long cannot be cast to javax.xml.datatype.XMLGregorianCalendar
-----begin dump of current state-----------------------------
Registered object factories: 2 (approximate size: 1,391.3 kB)
[AppleDto] : {Apple=DefaultConstructorObjectFactory<AppleDto>}
[MapEntry<Long, SeedDto>] : {Seed=ma.glasnost.orika.MapEntry_Seed_ObjectFactory314451582834800314451717205445$9@397fbdb}
-------------------------------------------------------------
Registered mappers: 4 (approximate size: 1,557.9 kB)
[0] : GeneratedMapper<Seed, SeedDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [], usedTypes: [] }
[1] : GeneratedMapper<Seed, MapEntry<Long, SeedDto>> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<Seed, SeedDto>], usedTypes: [] }
[2] : GeneratedMapper<Apple, AppleDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<Seed, MapEntry<Long, SeedDto>>, DefaultBoundMapperFacade<Seed, SeedDto>, DefaultBoundMapperFacade<Entry<Long, SeedDto>, Seed>], usedTypes: [Set<Seed>, ArrayList<MapEntry<Long, SeedDto>>, Map<Long, SeedDto>] }
[3] : GeneratedMapper<Entry<Long, SeedDto>, Seed> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<SeedDto, Seed>], usedTypes: [] }
-------------------------------------------------------------
Registered concrete types: 7 (approximate size: 181.0 kB)
[interface java.util.List] : ArrayList<Object>
[interface java.util.Set] : LinkedHashSet<Object>
[interface java.util.Map] : LinkedHashMap<Object, Object>
[interface java.util.Collection] : ArrayList<Object>
[interface java.util.SortedSet] : TreeSet<Object>
[interface java.util.Map$Entry] : MapEntry<Object, Object>
[interface java.util.SortedMap] : TreeMap<Object, Object>
-------------------------------------------------------------
Resolved strategies: 1 (approximate size: 1,558.5 kB)
{source: Apple, dest: AppleDto, in-place:false}: InstantiateAndUseCustomMapperStrategy<Apple, AppleDto> {customMapper: GeneratedMapper<Apple, AppleDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<Seed, MapEntry<Long, SeedDto>>, DefaultBoundMapperFacade<Seed, SeedDto>, DefaultBoundMapperFacade<Entry<Long, SeedDto>, Seed>], usedTypes: [Set<Seed>, ArrayList<MapEntry<Long, SeedDto>>, Map<Long, SeedDto>] }, unenhancer: ma.glasnost.orika.unenhance.BaseUnenhancer@5e0e82ae, objectFactory: DefaultConstructorObjectFactory<AppleDto>}
-------------------------------------------------------------
Unenhance strategy: ma.glasnost.orika.unenhance.BaseUnenhancer@5e0e82ae
-----end dump of current state-------------------------------
at ma.glasnost.orika.impl.ExceptionUtility.newMappingException(ExceptionUtility.java:55)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:681)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:650)
at testpackage.MappingTest.testSetToMapMapping(MappingTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to javax.xml.datatype.XMLGregorianCalendar
at ma.glasnost.orika.converter.builtin.DateAndTimeConverters$LongToXmlGregorianCalendarConverter.convertFrom(DateAndTimeConverters.java:408)
at ma.glasnost.orika.converter.BidirectionalConverter.convert(BidirectionalConverter.java:50)
at ma.glasnost.orika.MapEntry_Seed_ObjectFactory314451582834800314451717205445$9.create(MapEntry_Seed_ObjectFactory314451582834800314451717205445$9.java)
at ma.glasnost.orika.impl.DefaultBoundMapperFacade.newObject(DefaultBoundMapperFacade.java:204)
at ma.glasnost.orika.generated.Orika_AppleDto_Apple_Mapper314451653028796$6.mapAtoB(Orika_AppleDto_Apple_Mapper314451653028796$6.java)
at ma.glasnost.orika.impl.mapping.strategy.UseCustomMapperStrategy.map(UseCustomMapperStrategy.java:77)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:671)
... 25 more
我创建了一个展示问题的回购,我无法弄清楚我做错了什么。 https://github.com/dustins/orika-test