Wednesday, 2 March 2022

Dozer: Specify bean mapping using BeanMappingBuilder

We can configure the bean mappings using ‘BeanMappingBuilder’ class.

 

Step 1: Define an instance of BeanMappingBuilder.

BeanMappingBuilder beanMappingBuilder = new BeanMappingBuilder() {

	@Override
	protected void configure() {
		mapping(Employee.class, Person.class)
		.fields("fName", "firstName")
		.fields("lName", "lastName");

	}
};

 

Step 2: Define DozerBeanMapper instance and add mapping to BeanMappingBuilder instance.

DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(beanMappingBuilder);

 

 


Find the below working application.

 

Employee.java


 

package com.sample.app.model;

public class Employee {
	private Integer id;
	private String fName;
	private String lName;
	
	public Employee() {}

	public Employee(Integer id, String fName, String lName) {
		this.id = id;
		this.fName = fName;
		this.lName = lName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", fName=" + fName + ", lName=" + lName + "]";
	}
	
	

}

Person.java

package com.sample.app.model;

public class Person {
	private Integer id;
	private String firstName;
	private String lastName;

	public Person() {
	}

	public Person(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

BeanMappingBuilderDemo.java

package com.sample.app;

import java.io.IOException;

import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;

import com.sample.app.model.Employee;
import com.sample.app.model.Person;

public class BeanMappingBuilderDemo {

	public static void main(String[] args) throws IOException {

		BeanMappingBuilder beanMappingBuilder = new BeanMappingBuilder() {

			@Override
			protected void configure() {
				mapping(Employee.class, Person.class)
				.fields("fName", "firstName")
				.fields("lName", "lastName");

			}
		};

		DozerBeanMapper mapper = new DozerBeanMapper();
		
		mapper.addMapping(beanMappingBuilder);

		Employee employee = new Employee(1, "Arjun", "Gurram");
		Person person = mapper.map(employee, Person.class);

		System.out.println(employee);
		System.out.println(person);

	}

}

Output

Employee [id=1, fName=Arjun, lName=Gurram]
Person [id=1, firstName=Arjun, lastName=Gurram]


 

Previous                                                 Next                                                 Home

Dozer: Specify mappings via annotations

Using @Mapping annotation, we can define the mappings between to classes.

 

For example,

 

User.java

public class User {

	private Integer id;
	private String fName;
	private String lName;

	......
	......
}

 

Student.java

public class Student {
	private Integer id;

	@Mapping("fName")
	private String firstName;

	@Mapping("lName")
	private String lastName;

	.......
	.......
}

 

As you see above snippet, I specified the mappings using @Mapping annotation (this mapping is bi-directional).

 

a.   fName should be mapped to firstName

b.   lName should be mapped to lastName

 


Find the below working application.

 

 User.java

package com.sample.app.model;

public class User {

	private Integer id;
	private String fName;
	private String lName;

	public User() {
	}

	public User(Integer id, String fName, String lName) {
		this.id = id;
		this.fName = fName;
		this.lName = lName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", fName=" + fName + ", lName=" + lName + "]";
	}

}

 

Student.java

package com.sample.app.model;

import org.dozer.Mapping;

public class Student {
	private Integer id;

	@Mapping("fName")
	private String firstName;

	@Mapping("lName")
	private String lastName;

	public Student() {
	}

	public Student(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

 

CustomMappingWithAnnotations.java

package com.sample.app;

import java.io.IOException;

import org.dozer.DozerBeanMapper;

import com.sample.app.model.Student;
import com.sample.app.model.User;

public class CustomMappingWithAnnotations {

	public static void main(String[] args) throws IOException {

		DozerBeanMapper mapper = new DozerBeanMapper();

		User user1 = new User(1, "Arjun", "Gurram");
		Student student1 = mapper.map(user1, Student.class);
		User user2 = mapper.map(student1, User.class);

		System.out.println(user1);
		System.out.println(student1);
		System.out.println(user2);

	}

}

 

Output

User [id=1, fName=Arjun, lName=Gurram]
Student [id=1, firstName=Arjun, lastName=Gurram]
User [id=1, fName=Arjun, lName=Gurram]

 

  

Previous                                                 Next                                                 Home

Dozer: Specify bean mappings via xml file

In this post, I am going to explain how to specify bean mappings via xml file.

 

Define Employee class.

public class Employee {
	private Integer id;
	private String fName;
	private String lName;

	.....
	.....
}

 

Define Person class.

public class Person {
	private Integer id;
	private String firstName;
	private String lastName;

	.....
	.....
}

 

As you see Employee and Person class, both classes have a common property id, which can be auto mapped by dozer. But we need to explicitly specify the mapping logic to the properties fName, lName, firstName and lastName.

 

One way to specify custom mapping is via xml file.

 

employee-person-mapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">

	<mapping>
		<class-a>com.sample.app.model.Employee</class-a>
		<class-b>com.sample.app.model.Person</class-b>

		<field>
			<a>fName</a>
			<b>firstName</b>
		</field>

		<field>
			<a>lName</a>
			<b>lastName</b>
		</field>

	</mapping>

</mappings>

 

As you see above definition, I want fName to be mapped to firstName and lName to be mapped to lastName.

 

A mappings element has multiple mapping elements, each with class mapping declarations and field level mappings.

 

Set the mapping file to DozerBeanMapper instance before triggering map method.

 

DozerBeanMapper mapper = new DozerBeanMapper();

mapper.setMappingFiles(Arrays.asList("employee-person-mapping.xml"));

 

Find the below working application.


 

Employee.java

package com.sample.app.model;

public class Employee {
	private Integer id;
	private String fName;
	private String lName;
	
	public Employee() {}

	public Employee(Integer id, String fName, String lName) {
		this.id = id;
		this.fName = fName;
		this.lName = lName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", fName=" + fName + ", lName=" + lName + "]";
	}
	
	

}

 

Person.java

package com.sample.app.model;

public class Person {
	private Integer id;
	private String firstName;
	private String lastName;

	public Person() {
	}

	public Person(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

 

BeanMappingWithXMLConfig.java

package com.sample.app;

import java.io.IOException;
import java.util.Arrays;

import org.dozer.DozerBeanMapper;

import com.sample.app.model.Employee;
import com.sample.app.model.Person;

public class BeanMappingWithXMLConfig {

	public static void main(String[] args) throws IOException {

		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.setMappingFiles(Arrays.asList("employee-person-mapping.xml"));

		Employee employee = new Employee(1, "Arjun", "Gurram");
		Person person = mapper.map(employee, Person.class);

		System.out.println(employee);
		System.out.println(person);

	}

}

 

Output

Employee [id=1, fName=Arjun, lName=Gurram]
Person [id=1, firstName=Arjun, lastName=Gurram]

 


 

Previous                                                 Next                                                 Home