public class CustomerDTO {
private int customerId;
private String customerName;
private String customerAddress;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
}
CustomerDAO class:
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public final class CustomerDAO {
private CustomerDTO customer;
public void setCustomer(CustomerDTO customer) {
this.customer = customer;
}
//Trying to get copy of object with BeanUtils
public final CustomerDTO getCustomer(int customerId){
CustomerDTO origCustomer = _springContext.getBean(CustomerDTO.class);
CustomerDTO targetCustomer=null;
if("you get customer based on customer id") then "targetCustomer got initialized";
BeanUtils.copyProperties(targetCustomer, origCustomer);//spring BeanUtils
}
//Trying to add object returned by above method into the list
public final List<CustomerDTO> getCustomerList(List<Integer> customerIds){
List<CustomerDTO> customerList = new ArrayList<CustomerDTO>();
for(Integer id:customerIds){
CustomerDTO customer = getCustomer(id);
System.out.println("correct output: "+customer.getCustomerId());//getting correct output here
customerList.add(customer);//Trying to add copied object in list
}
for(CustomerDTO customer: customerList){
System.out.println("wrong output: "+customer.getCustomerId());//getting wrong output here
}
return Collections.unmodifiableList(customerList);
}
}
In CustomerDTO getCustomer(int customerId)
method, I am trying to return copy of CustomerDTO object by using Spring BeanUtils.copyProperties(targetCustomer, origCustomer);
, But when I am adding these copied objects in list in method List<CustomerDTO> getCustomerList(List<Integer> customerIds)
then I am getting strange behavior as mentioned in the comments. If I am removing BeanUtils.copyProperties(targetCustomer, origCustomer);
then behavior is correct.
Test case:
getCustomerList with customerIds =[1,2,3,4]
With copied objects: BeanUtils.copyProperties(targetCustomer, origCustomer);
//spring BeanUtils
correct output: 1
correct output: 2
correct output: 3
correct output: 4
wrong output: 4
wrong output: 4
wrong output: 4
wrong output: 4
Without copied objects: BeanUtils.copyProperties(targetCustomer, origCustomer);
//spring BeanUtils
correct output: 1
correct output: 2
correct output: 3
correct output: 4
wrong output: 1
wrong output: 2
wrong output: 3
wrong output: 4
Could someone please explain me what is wrong or possible explanation for this behavior?
Updated: Purpose of using BeanUtils:
I am trying to use defensive copy of the mutable object before returning the CustomerDTO object from the method getCustomer()
. So I try to use shallow cloning following this post.
update: Removed the word Immutability as it was wrong to use.