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:

public List<ReleveBancaireCreationRequestDomain> getReleveBancaires() {
    List<ReleveBancaireCreationRequestDomain> releveBancaireList = new ArrayList<>();
    List<ReleveBancaireEntity> releveBancaireEntityList = releveBancaireRepository.findAll();
    for (ReleveBancaireEntity r : releveBancaireEntityList) {
        ReleveBancaireCreationRequestDomain releveBancaire = new ReleveBancaireCreationRequestDomain();
        BeanUtils.copyProperties(r, releveBancaire);
        releveBancaireList.add(releveBancaire);
    }
    return releveBancaireList;
}

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

@Entity
public class ReleveBancaireEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long releveBancaireId;
    @CreationTimestamp
    @Temporal(TemporalType.DATE)
    private Date dateReception;
    private String label;
    private int nbrLignes;
    private int nbrOperationCredit;
    private int nbrOperationDebit;
    private BigDecimal soldeInitial;
    private BigDecimal soleFinal;
    @OneToMany(cascade= CascadeType.ALL,mappedBy = "releveBancaire", fetch = FetchType.EAGER)
    @JsonIgnoreProperties("releveBancaire")
    @JsonIgnore
    private List<LigneReleveEntity> lignereleve = new ArrayList<>();

and this is my model (DTO Model):

public class ReleveBancaireCreationRequestDomain {

    private String label;
    private int nbrLignes;
    private int nbrOperationCredit;
    private BigDecimal soldeInitial;
    private BigDecimal soleFinal;
    private List<LigneReleveCreationRequestDomain> lignereleve = new ArrayList<>();

This is the output I’m getting:

[
    {
        "label": "RELEVE_1",
        "nbrLignes": 15,
        "nbrOperationCredit": 7,
        "soldeInitial": 145.20,
        "soleFinal": 158.36,
        "lignereleve": []
    }
]

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

public interface StringValue {
  String getValue();
}

On each of your classes, implement the interface

public class LigneReleveCreationRequestDomain implements StringValue {

public class LigneReleveEntity implements StringValue {

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

private List<StringValue> lignereleve = new ArrayList<>();

private List<StringValue> lignereleve = new ArrayList<>();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement