3

In the below code, I have two objects of same class. One object (A - employeeObjectInDatabase) has all the fields set. Another object (B - employeeObjectForUpdate) has only few of the fields set. basically I need to set not null values of Object B to Object A. Below code has 'if not null' check for each field to set the value. Is there any other better way to get this done?

Is there a better way to replace the code between comments "BLOCK 1 BEGIN" and "BLOCK 1 END"?

In case of classes with few fields, checking for if not null is easy, but in case of 20+ fields there is a lot of if not null check required, hence thought of getting expert opinion.

Sample:

public class Employee {

    private String id;

    private String name;

    private String department;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }


    public static void main(String[] args) {

        Employee newEmployeeObject = new Employee();
        newEmployeeObject.setId("A100");
        newEmployeeObject.setName("Albert");
        newEmployeeObject.setName("Physics");

        //newEmployeeObject is persisted into database

        Employee employeeObjectForUpdate = new Employee();
        employeeObjectForUpdate.setId("A100");
        employeeObjectForUpdate.setName("Albert Einstein");

        //Inorder to update only the fields that are not null

        Employee employeeObjectInDatabase = employeeDao.getEmployee();

        //BLOCK 1 BEGIN
        if (null != employeeObjectForUpdate.getName())
            employeeObjectInDatabase.setName(employeeObjectForUpdate.getName());

        if (null != employeeObjectForUpdate.getDepartment())
            employeeObjectInDatabase.setDepartment(employeeObjectForUpdate.getDepartment());

        //BLOCK 1 END

        //persist employeeObjectInDatabase as it has the updated information
    }
}
Arun Chandrasekaran
  • 2,370
  • 2
  • 23
  • 33

4 Answers4

1

You can move the null check in your setter method

public void setName(String name) {
    if(name!=null){
        this.name = name;
    }
}

Then in your main method, you can you simply call the following: employeeObjectInDatabase.setName(employeeObjectForUpdate.getName());

If in your Data Access layer when you will populate the db values to your object, the same principle will be applied. That is if the Db column value is null, it wont be setting to the property

Ashish
  • 510
  • 3
  • 14
1

You could create a member method to transfer non-null field values to a given object of the same type using reflection.

blackcompe
  • 3,180
  • 16
  • 27
1

The following shows a solution using org.apache.commons.beanutils.BeanUtilsBean. See if it helps.

Helper in order to copy non null properties from object to another ? (Java)

Community
  • 1
  • 1
Tommy Siu
  • 1,265
  • 2
  • 10
  • 24
0

Add copy method to Object B. This method should copy its non null values to Object A and return it.And also you can use reflection in this method.

look at this post;

Copy all values from fields in one class to another through reflection

Community
  • 1
  • 1
Jman
  • 481
  • 1
  • 4
  • 16