简体   繁体   中英

Orika - No concrete class mapping defined for source

I have a Class1 which I want to convert to Class2.

Both Class1 and Class2 have a field: AbstractClass3 something .

AbstractClass3 is declared as abstract , but it's values are concrete implementations in Class1&2's fields.

When converting between Class1 and the Class2, I get this exception:

No concrete class mapping defined for source

I want Orika to use the same implementation in destination class (Class2) as the source class (Class1).

What is the best way to achieve this?

I tried to give a look at your problem but I think I may have missed something (or I may have not been understood correctly the problem) because it seems that by default Orika already uses the implementation of the source to create the implementation of the destination (if a classmap is configured), pls give a look at the code.

public class OrikaTest {

private MapperFacade mapper;
private MapperFactory factory;

@Before
public void setUp() {
    factory = new DefaultMapperFactory.Builder().mapNulls(false).build();
    factory.registerClassMap(factory.classMap(One.class, Two.class)
            .byDefault()
            .toClassMap());
    factory.registerClassMap(factory.classMap(Class3Impl1.class, Class3Impl1.class)
            .byDefault()
            .toClassMap());
    factory.registerClassMap(factory.classMap(Class3Impl2.class, Class3Impl2.class)
            .byDefault()
            .toClassMap());
    mapper = factory.getMapperFacade();
}

public static class One {
    public Class3 class3;
}

public static class Two {
    public Class3 class3;
}

public static abstract class Class3 {
    public String x = "x";
}

public static class Class3Impl1 extends Class3 {
    public String y = "y";
}

public static class Class3Impl2 extends Class3 {
    public String y = "y";
}

@Test
public void mapping1Test() {
    One one = new One();
    one.class3 = new Class3Impl1();

    Two two = mapper.map(one, Two.class);

    assertEquals(one.class3.x, two.class3.x);
    assertTrue(Class3Impl1.class.isInstance(two.class3));
    assertEquals(((Class3Impl1) one.class3).y, ((Class3Impl1) two.class3).y);
}

@Test
public void mapping2Test() {
    One one = new One();
    one.class3 = new Class3Impl2();

    Two two = mapper.map(one, Two.class);

    assertEquals(one.class3.x, two.class3.x);
    assertTrue(Class3Impl2.class.isInstance(two.class3));
    assertEquals(((Class3Impl2) one.class3).y, ((Class3Impl2) two.class3).y);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM