Skip to content

Regex Mapping

Alessandro Vurro edited this page Jun 8, 2016 · 8 revisions

The regex come in handy when we have to configure the fields with similar names, instead of defining every relationship, using the regex we can dramatically reduce the setup.
The following is a sample configuration with regex:

class Source1{

   List<String> customersIds;

   // getter and setter...
}

class Source2{

   Set<Integer> companyIds;

   // getter and setter...
}

class Source3{

   TreeSet<String> supplierIds;

   // getter and setter...
}

As you can see the fields have different structures, JMapper handles all implicitly.

class Destination{

   @JMap(".*ids$")
   List<Integer> ids;

   // getter and setter...
}

Xml configuration:

<jmapper>
  <class name="package.Destination">
    <attribute name="ids">
      <value name=".*ids$"/>
    </attribute>
  </class>
</jmapper>

API configuration:

...
import static com.googlecode.jmapper.api.JMapperAPI.*;
...

JMapperAPI jmapperAPI = new JMapperAPI()
    .add(mappedClass(Destination.class)
             .add(attribute("ids")
                     .value(".*ids$")));

You can see a complete example to the Relational mapping wiki page.

Clone this wiki locally