Convert DTO to Entity Java

1

I have the following problem, I am trying to create a method that converts DTO's to Entity's and vice versa until the moment I arrived at the following method that converts perfectly when they are compatible attributes String to String , Long to Long , and so on. Of course the premise is that the types I want to convert are always the same.

public static <T> void ConvertDtoToEntity(T dto, T entity){
    Field[] entitycampos = entity.getClass().getDeclaredFields();
    Field[] dtocampos = dto.getClass().getDeclaredFields();

    for (Field entityfield : entitycampos) {
        entityfield.setAccessible(true);
        for (Field dtofield : dtocampos) { 
            dtofield.setAccessible(true);               
            if(!entityfield.getName().equals("serialVersionUID") 
                && entityfield.getName().equals(dtofield.getName()))
            {
                try {
                    entityfield.set(entity, dtofield.get(dto));
                    break;
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Only the problem is when converting List, Set and or other dts or other entities that are within that dto. EX DTO that convert perfectly to an entity:

public class CupomHistoricoDto implements Serializable{

    private static final long serialVersionUID = 1L;

    private BigDecimal seqcupomhistorico;
    private int quantidade;
    private Date data;
    private long nroempresa;

EX Entity that convert perfectly to a dto:

public class CupomHistoricoEntity implements Serializable {

    private static final long serialVersionUID = 1L;    

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "SEQCUPOMHISTORICO", unique = true, nullable = false)
    private BigDecimal seqcupomhistorico;

    @Column(name = "QUANTIDADE", nullable = false)
    private int quantidade;

    @Column(name = "DATA", nullable = false)
    private Date data;

    @Column(name = "NROEMPRESA", nullable = false)
    private long nroempresa;

EX DTO that is not converting to an entity

public class CupomHistoricoDto implements Serializable{

    private static final long serialVersionUID = 1L;

    private BigDecimal seqcupomhistorico;
    private int quantidade;
    private Date data;
    private long nroempresa;
    private CupomHistoricoDetalheDto detalhe; 

EX Entity

public class CupomHistoricoEntity implements Serializable {

    private static final long serialVersionUID = 1L;    

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "SEQCUPOMHISTORICO", unique = true, nullable = false)
    private BigDecimal seqcupomhistorico;

    @Column(name = "QUANTIDADE", nullable = false)
    private int quantidade;

    @Column(name = "DATA", nullable = false)
    private Date data;

    @Column(name = "NROEMPRESA", nullable = false)
    private long nroempresa;

    @OneToOne  
    @JoinColumn(name = "seqdetalhe", referencedColumnName = "SEQDETALHE")
    private CupomHistoricoDetalheEntity detalhe;

Someone would have some idea how to implement this method or have already done something similar.

NOTE: I am testing if I can do what I want using Apache's BeanUtils.

    
asked by anonymous 17.03.2015 / 15:59

2 answers

1

I think the best thing for you is to do it manually. Create your own mechanism for doing this conversion, since you know the DTOs and Entitys perfectly and as you yourself said: "We use many fields in the dtos that are impertinent in the entity." With a mechanism created by you and specific you can control what goes from DTO to Entity and vice versa since not everything from DTO has to pass to respective Entity. Do something like this:

  • Create an interface

    public interface Conveter
    {   
       public GenericDTO convertFromEntity(GenericEntity entity);
       public GenericEntity convertFromDTO(GenericDTO dto);
    }
    
  • Create implementations for each of your types (DTO-Entity)

     public CupomHistoricoConverter implements Converter
     {
        @Override
        public GenericDTO convertFromEntity(GenericEntity entity)
        {
          // Faça aqui a conversão manual campo a campo de entity para dto
        }
    
        @Override
        public GenericEntity convertFromDTO(GenericDTO dto)
        {
          // Faça aqui a conversão manual campo a campo de  dto para entity
        }
    } 
    
  • It's a little hard work, but it's better because there you have total control of what's happening and what's really to be converted.

    I hope I have helped

        
    24.03.2015 / 08:41
    0

    Following the rationale of the previous solution, we can still leave parameterized to avoid casting when implementing the converters.

    public interface Converter<DTO extends GenericDTO, ENT extends GenericEntity> {
    
            public DTO convertFromEntity(ENT entity);
            public ENT convertFromDTO(DTO dto);
    
    }
    
    
    
    public CupomHistoricoConverter implements Converter <CupomHistoricoDTO, CupomHistoricoEntity> 
     {
        @Override
        public CupomHistoricoDTO convertFromEntity(CupomHistoricoEntity entity)
        {
          // Faça aqui a conversão manual campo a campo de entity para dto
        }
    
        @Override
        public CupomHistoricoEntity convertFromDTO(CupomHistoricoDTO dto)
        {
          // Faça aqui a conversão manual campo a campo de  dto para entity
        }
    } 
    
        
    10.10.2015 / 21:40