Skip to content
Advertisement

How to copy object that has a list with BeanUtils?

I’m working on a multi maven modules and I want to copy from entity to model with BeanUtils, here what I tried:

JavaScript

This is my entity (with getters/setter/noargs/allargs):

JavaScript

and this is my model (DTO Model):

JavaScript

This is the output I’m getting:

JavaScript

The problem is that the fields get copied but the list does not it shows me an empty List. Is there any solution please.

Advertisement

Answer

The reason the list isn’t being copied is that they are of different types. In your source object that list is of type LigneReleveEntity, in your target that list is of type LigneReleveCreationRequestDomain.

If you want to use the copy, then these either need to be of the same type, or implement the same interface.

Without knowing what those classes contain, here’s a simple example assuming that they contain a single String value in a field called value.

Create an interface

JavaScript

On each of your classes, implement the interface

JavaScript

JavaScript

In your wrapper classes, reference these attributes by the interface and not the implementation:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement