3

I want to map constants from a Java class when mapping two classes using Dozer like shown in the example mapping shown for field 2 below. Is there a cleaner approach to do this in Dozer apart from Factory Bean instances..

     <mapping>
    <class-a>com.mod.Class1</class-a>
    <class-b>com.mod.Class2</class-b>
    <field>
        <a>field1inClass1</a>
        <b>field1inClass2</b>
    </field>
    <field>       <!-- This is from constants. -->
        <a>constants.Field</a>
        <b>field2inClass2</b>
    </field>
   </mapping> 

1 Answer 1

2

I have needed this in the past too but no success, Dozer doesn't support mapping to a constant value in a class different from the ones defined in class-a or class-b. If you would have your constant defined inside a class in class-a or class-b, you would only have to do a one way mapping and use is-accessible="true" in the constant field.

As an ugly workaround, you can create another mapping element to do one way mapping from you Constants class to your destination object, but you would have to pass Dozer an instance of your constants class (I know, that's bad) and this is only possible if your Constants class is instantiable (which is worst).

<mapping>
    <class-a>ConstantsClass</class-a>
    <class-b>InstantiableClass</class-b>        
    <field type="one-way">           
        <a is-accessible="true">THE_CONSTANT</a>
        <b>value</b> 
    </field>        
</mapping>

Related topic on Dozer forums

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.