- Given a source bean or object in java.
- We would like to copy properties from source bean into destination bean in java.
- i.e. We would like to convert one object into another object.
1. Example: Convert one object into another object in java (copy properties)
- Given an EmployeeDB object containing information about employees, present in database table.
- EmployeeDB bean may contains fields like firstName,lastName, age, salary, createdDate, modifiedDate
- We do not want to send all employee related information (present in database) to presentation layer.
- We would like to convert EmployeeDB object into tailored Employee object or EmployeeDTO.
- EmployeeDTO object will contain few fields like firstName, lastName & age (Only these fields may be relevant for GUI applications).
- i.e. We want to achieve EmployeeDB -> EmployeeDTO conversion.
- We would use commons-beanutils jars to copy properties from source bean into destination bean.
- Where, source bean is EmployeeDB & destination bean is EmployeeDTO.
2. Maven dependency of commons-beanutils jar:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
3. Copy properties from source bean into destination bean (java/example)
3.1 Source bean or Object (EmployeeDB):
- Source bean or EmployeeDB object containing required fields is as follows:
package org.learn;
import java.util.Date;
public class EmployeeDB {
private String firstName;
private String lastName;
private int age;
private int experience;
private int salary;
private Date createdDate;
private Date modifiedDate;
public EmployeeDB(String firstName, String lastName, int age, int experience,
int salary, Date createdDate, Date modifiedDate) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.experience = experience;
this.salary = salary;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
@Override
public String toString() {
return String.format("[Name:%s %s,Age:%d,Salary:%d,Experience:%d]",
firstName,lastName,age,salary,experience);
}
}
3.2 Destination bean or Object (EmployeeDTO):
- Destination bean or EmployeeDTO object containing required properties is as follows:
package org.learn;
public class EmployeeDTO {
private String firstName;
private String lastName;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return String.format("[Name:%s %s,Age:%d]",
firstName,lastName,age);
}
}
3.3 Copy Properties from source bean to destination bean:
- DemoCopyProperties class will copy properties from source object to destination object.
- We will use commons-beanutils jar to copy properties (i.e. from source bean to destination)
package org.learn;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
public class DemoCopyProperties {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
//Create destination object
EmployeeDTO employeeDTO = new EmployeeDTO();
//Simulate database operation
EmployeeDB employeeFromDB = getObjectFromDatabase();
System.out.println("1. EmployeeDB Object (Source bean) from database :");
System.out.println(employeeFromDB);
BeanUtils.copyProperties(employeeDTO,employeeFromDB);
System.out.println("2. Convert EmployeeDB (source) object to EmployeeDTO (destination bean):");
System.out.println(employeeDTO);
}
private static EmployeeDB getObjectFromDatabase() {
EmployeeDB employeeDB = new EmployeeDB("Mike","Young", 22,2,
10000, new Date(),new Date());
return employeeDB;
}
}
4. Output: convert one object into another object (java/example)
1. EmployeeDB Object (Source bean) from database :
[Name:Mike Young,Age:22,Salary:10000,Experience:2]
2. Convert EmployeeDB (source) object to EmployeeDTO (destination bean):
[Name:Mike Young,Age:22]