Skip to content

Global Mapping

avurro edited this page Jan 13, 2016 · 12 revisions

In some cases we have the need to map two or more fields toward the same field.
Currently the only mode to do this is configure each field creating much redundance.
To avoid it, will be introduced the global mapping.

Annotation

The annotation to use is @JGlobalMap and must be applied to the class, it has the parameters:

  • value, it permits to define the target field name
  • classes, are the classes to which the field must belong
  • attributes,are the fields of the current class that will be part of the mapping
  • excluded, are the fields of the current class that will be excluded from mapping

1° example

With @JMap:

class Bean {
           
   @JMap("properties") String field1;
   @JMap("properties") String field2;
   String field3;
               
  //getters and setters..
}

With @JGlobalMap:

@JGlobalMap(value="properties",excluded={"field3"})
// or @JGlobalMap(value="properties",attributes={"field1","field2"})
class Bean {
           
   String field1;
   String field2;
   String field3;
            
   //getters and setters..
}

2° example

With @JMap:

class Bean {
           
   @JMap String field1;
   @JMap String field2;
   String field3;
               
   //getters and setters..
}

With @JGlobalMap:

@JGlobalMap(excluded={"field3"})
// or @JGlobalMap(attributes={"field1""field2"})
class Bean {
           
    String field1;
    String field2;
    String field3;

    //getters and setters..
}

In this case the mapping isn't toward one field, but each field is mapped toward a field with his name.

@JGlobalMap has greater visibility of @JMap, if a field is not configured with @JGlobalMap JMapper checks if it is configured with @JMap.

For example:

@JGlobalMap(excluded={"field3"})
class Bean {
           
    String field1;
    String field2;
    @JMap("other")
    String field3;
               
    //getters and setters..
}

XML format

The tag to use is:<global>.
It has the same structure of <attribute> node, but without the attribute name and with a more node: <excluded>.
Considering the class written in advance, see the following examples.

1° example

The local fields have the same target field names.

With <attribute>:

<jmapper>
   <class name = "Bean">
      <attribute name = "field1">
         <value name = "field1"/>
      </attribute>
      <attribute name = "field2">
         <value name = "field2"/>
      </attribute>
   </class>
</jmapper>

With <global>:

<jmapper>
   <class name = "Bean">
      <global>
         <excluded>
            <attribute name = "field3"/>
         </excluded>
      <!-- or 
         <attributes>
            <attribute name = "field1"/>
            <attribute name = "field2"/>
         </attributes>
      -->
      </global>
   </class>
</jmapper>

2° example

Explicit target field.

With <attribute>:

<jmapper>
   <class name = "Bean">
      <attribute name = "field1">
         <value name = "properties"/>
      </attribute>
      <attribute name = "field2">
         <value name = "properties"/>
      </attribute>
   </class>
</jmapper>

With <global>:

<jmapper>
   <class name = "Bean">
      <global>
         <value name = "properties"/>
         <exclude>
            <attribute name = "field3"/>
         </excluded>
      <!-- or 
         <attributes>
            <attribute name = "field1"/>
            <attribute name = "field2"/>
         </attributes>
      -->
      </global>
   </class>
</jmapper>

<global> has greater visibility of <attribute>, if a field is not configured with <global> JMapper checks if it is configured with <attribute>.
For example:

<jmapper>
   <class name = "Bean">
      <global>
         <value name = "properties"/>
            <excluded>
               <attribute name = "field3" />
  	    </excluded>
      </global>
      <attribute name = "field3">
         <value name = "other"/>
      </attribute>
   </class>
</jmapper>
Clone this wiki locally