简体   繁体   中英

Mapping abstract class in dozer

I have the following class structure (it actually is a VO layer with Hibernate mappings):

public abstract class abstractClassVO {
    private int id;
    private String name;
}

public class concreteClassAVO extends abstractClassVO {
    private String aAttribute;
}

public class concreteClassBVO extends abstractClassVO {
    private Long bAttribute;
}

And the equivalent DTO objects:

public abstract class abstractClassDTO {
    private int id;
    private String name;
}

public class concreteClassADTO extends abstractClassDTO {
    private String aAttribute;
}

public class concreteClassBDTO extends abstractClassDTO {
    private Long bAttribute;
}

Then I have another object like this:

public class compositeObject {
     private int anAttribute;
     private abstractClassVO myInstance;

}

and its equivalent:

public class compositeObjectDTO{
    private int anAttribute;
    private abstractClassDTO myInstance;
}

How can I tell dozer to automatically map myInstance to the specific DTO that corresponds to the concrete class implementation in the VO layer?

Currently, out of the box, Dozer isn't even putting anything in the myInstance field of the compositeObjectDTO class. My guess is that it's due to the fact that abstractClassDTO it is an abstact class, and since it cannot determine the implementation, it does nothing. I am not getting any exceptions.

Dozer can't do it out of the box but you could write a helper that would determine destination class by source class. You can get this information from DozerBeanMapper.getMappingMetadata().getClassMappings* methods. These methods return list of ClassMappingMetadata that contains destination class. You just only need to chech whether destination class is inherited from abstractClassDTO. This check can be omitted if you only have one mapping for one VO.

For bi-directional mapping you should additionally check ClassMappingMetadata.MappingDirection field.

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