Site icon

Copy properties from one bean into another bean/object (java/ example)

1. Example: Convert one object into another object in java (copy properties)

  1. Given an EmployeeDB object containing information about employees, present in database table.
    • EmployeeDB bean may contains fields like firstName,lastName, age, salary, createdDate, modifiedDate
  2. We do not want to send all employee related information (present in database) to presentation layer.
  3. 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.
  4. 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):

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):

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:

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]

Download code - copy bean properties source destination java example

Exit mobile version