Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping constant values in Dozer

Tags:

dozer

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> 
like image 845
Greg Avatar asked Aug 06 '26 17:08

Greg


1 Answers

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

like image 153
davidmontoyago Avatar answered Aug 10 '26 21:08

davidmontoyago



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!