Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - how to simplify my java
    primarykey
    data
    text
    <p>I am writing some java code in the Spring Framework.</p> <p>I've got two beans, person and person1. They have a slightly different structure, that is to say the variable names for each differ slightly. </p> <p>I'm trying to copy the details from one bean to the other. I only want to copy the value if the value is not null. I've seen an API called BeanUtils, but this will copy it regardless if it is null or not. </p> <p>Here's my code:</p> <pre><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()); } } } </code></pre> <p>I have about 40 nodes that need to be copied over and this would create so much ugly code. Does anyone have a better way to do this? maybe if i make a mapping or something and then loop through it? not sure.</p> <p>If not, does anyone know if i can make BeanUtils run a copy without copying the null values?</p> <p>Reason is that the second bean, person1, already has a bunch of values. I only want to overwrite that if there are new values to overwrite it with. </p> <p>As usual the variables are part of a much larger system and I can't standardise the names.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload