How to map a list of objects in a list of primitives using Modelmapper

Asked

Viewed 6 times

-1

I have the following class in java:

@Entity
@Table(name = "tbcs_localatendimento", schema = "cats")
public class LocalAtendimento {
...
    
    @NotNull
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "tbcs_localatendacessibilidade", schema = "cats", 
        joinColumns = { @JoinColumn(name = "ccsloc_codigo") }, 
        inverseJoinColumns = { @JoinColumn(name = "ccsacs_codigo") })
    private Set<Acessibilidade> acessibilidades = new HashSet<>();
    
    
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "tbcs_localatendcomodidade", schema = "cats", 
        joinColumns = { @JoinColumn(name = "ccsloc_codigo") }, 
        inverseJoinColumns = { @JoinColumn(name = "ccscom_codigo") })
    private Set<Comodidade> comodidades = new HashSet<>();
    
    
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "tbcs_localatendrestricao", schema = "cats", 
        joinColumns = { @JoinColumn(name = "ccsloc_codigo") }, 
        inverseJoinColumns = { @JoinColumn(name = "ccsres_codigo") })
    private Set<RestricaoLocal> restricoes = new HashSet<>();
    
    
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "tbcs_localatendequipamento", schema = "cats", 
        joinColumns = { @JoinColumn(name = "ccsloc_codigo") }, 
        inverseJoinColumns = { @JoinColumn(name = "ccsequ_codigo") })
    private Set<Equipamento> equipamentos = new HashSet<>();
}

I would like to know how to map each object in the list above in a list of primitive types using the Modelmappper, for example:

@Service
@Transactional
public class LocalAtendimentoHistoricoManager {

    @Autowired
    private ModelMapper modelMapper;

    public void insert(LocalAtendimento element)  {
        LocalAtendimentoHistorico historico = modelMapper.map(element, LocalAtendimentoHistorico.class);
        insert(historico);
    }

}

Target class of mapping:

@Entity
@IdClass(value = LocalAtendimentoHistoricoPK.class)
@Table(name = "tbcsh_localatendimento", schema = "cats")
public class LocalAtendimentoHistorico {
...

    @ElementCollection
    @CollectionTable(schema = "cats", name = "tbcsh_localatendacessibilidade", joinColumns = {
            @JoinColumn(name = "ccshla_codigo", referencedColumnName = "ccshla_codigo"),
            @JoinColumn(name = "tcshla_ulat", referencedColumnName = "tcshla_ulat") })
    @Column(name = "ccshae_codigo_acs")
    private Set<Integer> acessibilidadesId = new HashSet<>();

    @ElementCollection
    @CollectionTable(schema = "cats", name = "tbcsh_localatendcomodidade", joinColumns = {
            @JoinColumn(name = "ccshla_codigo", referencedColumnName = "ccshla_codigo"),
            @JoinColumn(name = "tcshla_ulat", referencedColumnName = "tcshla_ulat") })
    @Column(name = "ccshlc_codigo_com")
    private Set<Integer> comodidadesId = new HashSet<>();

    @ElementCollection
    @CollectionTable(schema = "cats", name = "tbcsh_localatendrestricao", joinColumns = {
            @JoinColumn(name = "ccshla_codigo", referencedColumnName = "ccshla_codigo"),
            @JoinColumn(name = "tcshla_ulat", referencedColumnName = "tcshla_ulat") })
    @Column(name = "ccshlr_codigo_res")
    private Set<Integer> restricoesId = new HashSet<>();

    @ElementCollection
    @CollectionTable(schema = "cats", name = "tbcsh_localatendequipamento", joinColumns = {
            @JoinColumn(name = "ccshla_codigo", referencedColumnName = "ccshla_codigo"),
            @JoinColumn(name = "tcshla_ulat", referencedColumnName = "tcshla_ulat") })
    @Column(name = "ccshle_codigo_equ")
    private Set<Integer> equipamentosId = new HashSet<>();

}

Additional information: I am using Modelmapper’s standard mapping strategy and would like to avoid as much as possible such solutions:

element.getAcessibilidades().forEach(a -> {
    historico.getAcessibilidadesId().add(a.getId());
});

Instead I would like to make use of the Modelmapper api

Grateful

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.