Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Orika-如何映射抽象嵌套属性';s场_Java_Mapping_Orika - Fatal编程技术网

Java Orika-如何映射抽象嵌套属性';s场

Java Orika-如何映射抽象嵌套属性';s场,java,mapping,orika,Java,Mapping,Orika,嗨,我很难映射抽象嵌套属性的字段。这里我有一个更好地解释它的测试用例: package com.mycompany.asd; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.MappingException; import ma.glasnost.orika.impl

嗨,我很难映射抽象嵌套属性的字段。这里我有一个更好地解释它的测试用例:

package com.mycompany.asd;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingException;
import ma.glasnost.orika.impl.DefaultMapperFactory;

/**
 * Tests the mapping of abstract nested property's fields
 */
public class OrikaTest {

private MapperFactory mapperFactory;

@Before
public void createMapperFactoryAndDefineMapping() {
    mapperFactory = new DefaultMapperFactory.Builder()
            .useAutoMapping(false).build();
    mapperFactory.classMap(FakeBeanA.class, FakeBeanB.class)
            .field("fieldA", "fieldB.nestedField")
            .field("fieldA2", "fieldB.nestedField2") // Problem here.
            .register();
}

@Test(expected = MappingException.class)
public void cannotMapAbstractNestedPropertyWhenConcreteTypeIsNotRegistered() {

    // We expect to get a MappingException. Indeed, Orika doesn't know how
    // to create an
    // instance of AbstractNestedField (the type of FakeBeanB.fieldB)
    createAnInstanceOfFakeBeanAAndMapItToFakeBeanB();
}

@Test
public void mapAbstractNestedPropertyWhenConcreteTypeIsRegistered() {

    // Register concrete type for AbstractNestedType
    mapperFactory.registerConcreteType(AbstractNestedType.class,
            NestedType.class);

    // Orika should be able to create an instance of FieldB abstract type
    // (as we have explicitly defined above the concrete type to create and
    // the SimpleConstructorResolverStrategy is normally able to create an
    // instance of this concrete type)
    // Therefore, the mapping should work !
    createAnInstanceOfFakeBeanAAndMapItToFakeBeanB();
}

private void createAnInstanceOfFakeBeanAAndMapItToFakeBeanB() {

    // Create an instance of FakeBeanA and assign a value to its fieldA
    FakeBeanA fakeBeanA = new FakeBeanA();
    fakeBeanA.fieldA = 42;

    // Try the mapping from fakeBeanA to FakeBeanB
    FakeBeanB fakeBeanB = mapperFactory.getMapperFacade().map(fakeBeanA,
            FakeBeanB.class);

    // Assert the fieldA has been correctly mapped to fieldB.nestedField
    Assert.assertEquals(fakeBeanA.fieldA, fakeBeanB.fieldB.nestedField);
}

public static class FakeBeanA {
    public int fieldA;
    public int fieldA2;
}

public static class FakeBeanB {
    public AbstractNestedType fieldB;
}

public static class NestedType extends AbstractNestedType {
    public int nestedField2; // NEW ADDED
}

public static abstract class AbstractNestedType {
    public int nestedField;
}

}这是不可能的,也没有意义,如果AbstractNestedType的另一个子类没有nestedField2怎么办

你能做的就是使用

.customize(new CustomMapper<> {
    void mapBToA(FakeBeanA a, FakeBeanB b) {
   if(b.fieldB instanceof NestedType) {
      a.fieldA2 = ((NestedType)b.fieldB).nestedField2;
   }
 }});
.customize(新的CustomMapper){
void mapBToA(FakeBeanA a、FakeBeanB b){
if(b.fieldB嵌套类型的实例){
a、 fieldA2=((NestedType)b.fieldB).nestedField2;
}
}});