I am writing java code in Spring Framework.
I have two beans, human and human1. They have a slightly different structure, that is, the variable names for each of them are slightly different.
I am trying to copy data from one bean to another. I only want to copy the value if the value is not equal to zero. I have seen an API called BeanUtils, but this will copy it whether it is null or not.
Here is my code:
if (person != null) {
if (person.getAddressDetails() != null) {
if (person.getAddressDetails().getStreetNumber() != null) {
person1.getAddressDetails().setStreetNo(person.getAddressDetails().getStreetNumber());
}
if (person.getAddressDetails().getStreetName() != null) {
person1.getAddressDetails().setStreetName(person.getAddressDetails().getStreetName());
}
}
if (person.getHomeDetails() != null) {
if (person.getHomeDetails().getPhoneNumber() != null) {
person1.getHomeDetails().setSPhoneNo(person.getHomeDetails().getPhoneNumber());
}
}
}
I have about 40 nodes that need to be copied and this will create such ugly code. Anyone have a better way to do this? maybe if I do a mapping or something else and then go through it? not sure.
If not, does anyone know if I can get BeanUtils to run a copy without copying the null values?
, bean, person1, . , , .
, , .