BeanUtils.copyProperties missing deeply nested variables?

Lancelot picture Lancelot · Jun 5, 2009 · Viewed 19.2k times · Source

I'm using BeanUtils.copyProperties to copy the entire content of one object into another that inherit from it.

Here is the context, the domain object from which the values are copied contains a Set of objects of custom type Xref. That custom type has an embedded class with various fields of various class types.

For some reason one of the field from an object encapsulated within the embedded object does not get copied over. But most everything else I need do get copied over.

With an example:

class Source {
private Set<Xref> xref;
...
}

class Xref {
...
public static class primaryKey {
...
private MyObj obj;
}
}

class MyObj {
private Integer id;
...
}

Using those names if I try to use BeanUtils.copyProperties to copy the content of a "Source" object into a "SourceExtended" object the value of source.xrefs.get(0).getPrimaryKey().getObj().getId() does not get copied over. In the original object it has a value but in the target object it's null...

Any idea why???

Thank you.

Answer

user7094 picture user7094 · Jun 5, 2009

From the Javadocs:

Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.