2017-02-23 68 views
0

Null值在我的Java項目中,我使用org.dozer.BeanMapper一個複雜的源對象合併到與同類型目標對象:DozerBeanMapper:忽略來自源

sourceObject.valueA = null 
sourceObject.valueB = B1 
sourceObject.valueC = C1 

destinationObject.valueA = A2 
destinationObject.valueB = null 
destinationObject.valueC = C2 

beanMerger.map(sourceObject, destinationObject); 

這將導致該值我destinationObject:

destinationObject.valueA: null 
destinationObject.valueB: B1 
destinationObject.valueC: C1 

我可以配置BeanMapper的方式,它不應該忽略一個空值的字段?我期望的結果應該是:

destinationObject.valueA: A2 
destinationObject.valueB: B1 
destinationObject.valueC: C1 

甚至更​​好(不要目標對象的觸摸設定值)

destinationObject.valueA: A2 
destinationObject.valueB: B1 
destinationObject.valueC: C2 

回答

1

我找到了解決辦法:

beanMerger.addMapping(
    new BeanMappingBuilder() { 
    protected void configure() { 
     mapping(MyObjectDTO.class, MyObjectDTO.class, TypeMappingOptions.mapNull(false)); 
    } 
    });