2012-03-11 40 views
2

在我的应用我有推土机的映射,看起来像这样:如何将`this`传递给Dozer字段映射?

<mapping> 
    <class-a>java.util.HashMap</class-a> 
    <class-b>org.mycompany.TargetClass</class-b> 
    <field custom-converter="org.example.MyConverter"> 
     <a>this</a> 
     <b>anotherField</b> 
    </field> 
</mapping> 

MyConverterConfigurableCustomConverter一个实例:

public class MyConverter implements ConfigurableCustomConverter { 

    private String parameter; 

    @Override 
    public Object convert(
      Object existingDestinationFieldValue, 
      Object sourceFieldValue, 
      Class<?> destinationClass, 
      Class<?> sourceClass) { 
     // sourceClass is always java.lang.Object and 
     // sourceFieldValue is always null!!! 
    } 

    @Override 
    public void setParameter(String parameter) { 
     this.parameter = parameter; 
    } 
} 

为什么事情指出,在源评论发生的呢?

回答

3

你需要告诉推土机该地图的关键应该被映射到b.anotherField,喜欢的东西

<field custom-converter="org.example.MyConverter"> 
    <a key="foobar">this</a> 
    <b>anotherField</b> 
</field> 

http://dozer.sourceforge.net/documentation/mapbackedproperty.html#Mapping_Class_Level_Properties_to_a_java.util.Map_or_a_Custom_Map_with_unique_GetSet_methods

+3

但是,有没有办法通过映射对象(或地图)本身,而不是一个独特的领域? – 2012-03-12 06:26:48

+0

“这个”是天赐之物。谢谢 – 2016-03-03 11:37:03