Professional Documents
Culture Documents
Overview
Orika uses a declarative Java-based configuration of mappings from one class to another, whereby you define which fields from one type need to be
matched up with which fields from another using a fluent-style API.
For many cases, Orika can properly generate a suitable mapper using just the names of the fields you provide—by investigating their respective types
using reflection. If the names of the fields you need to map match, you don’t even need to explicitly declare them—the byDefault() method of
ClassMapBuilder will match these for you.
class BasicPerson {
private String name;
private int age;
private Date birthDate;
// getters/setters omitted
}
class BasicPersonDto {
private String fullName;
private int currentAge;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
private Date birthDate;
// getters/setters omitted
}
In order to map between these classes, we’ll need to first register a ClassMap for the two types, which is created like so:
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("name", "fullName")
.field("age", "currentAge")
.register();
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("name", "fullName")
.field("age", "currentAge")
.byDefault()
.register();
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.fieldAToB("name", "fullName")
...
.register();
This would result in the ‘name’ field of BasicPerson being copied to the ‘fullName’ field of BasicPersonDto, but does not include mapping ‘fullName’ to
‘name’ when mapping is in the other direction.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Excluding a field from mapping
To explicitly exclude a field from mapping, use the exclude(name), like so:
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.exclude("name")
...
.register();
In this example, we specify that the constructor with parameters { “name”, “id” } should be used to instantiate instances of BasicPerson:
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.constructorA("name","id")
...
.register();
class BasicPerson {
private List<String> nameParts;
// getters/setters omitted
}
class BasicPersonDto {
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
private String firstName;
private String lastName;
// getters/setters omitted
}
We could map the first element of BasicPerson’s ‘nameParts’ to BasicPersonDto’s ‘firstName’ property, and the second element to ‘lastName’, like so:
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("nameParts[0]", "firstName")
.field("nameParts[1]", "lastName")
.register();
Note that the same structure would work if ‘nameParts’ were defined as a `String[]`
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("nameParts['first']", "firstName")
.field("nameParts[\"last\"]", "lastName")
.register();
class Name {
private String first;
private String last;
private String fullName;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
// getters/setters
}
class BasicPerson {
private Name name;
// getters/setters omitted
}
class BasicPersonDto {
private String firstName;
// getters/setters omitted
}
Then the following syntax could be used to map the nested “first” property of BasicPerson’s “name” to the “firstName” property of BasicPersonDto:
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("name.first", "firstName")
.register();
class Name {
private String first;
private String last;
private String fullName;
// getters/setters
}
class Person {
private List<Name> names;
// getters/setters
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
}
class PersonDto {
private Map<String, Name> personalNames;
private String[] firstNames;
private List<String> lastNames;
// getters/setters omitted
}
If we wanted to map the ‘names’ property from Person into the Map ‘personalNames’ in PersonDto, with the ‘fullName’ property (of the Name element)
as the key, and the Name element itself as the value, we could accomplish it with the following syntax:
mapperFactory.classMap(Person.class, PersonDto.class)
.field("names{fullName}", "personalNames{key}")
.field("names{}", "personalNames{value}")
.register();
The element of a Map is assumed to be of type Map.Entry, so we can refer to either the ‘key’ or ‘value’ property of this element.
Nested properties are legal within the braces, and so are nested element references, in case the element type itself also contains multi-occurrence
fields that you want to map.
The empty field name within the braces (“names{}”) is used to refer to the element itself (rather than some property of the element).
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD