1
I have the following problem, I am trying to create a method that converts DTO’s for Entity’s and vice versa until the moment I arrived at the following method that converts perfectly when they are compatible attributes String for String, Long for Long etc. Of course being the premise 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 time to convert List, Set and other dtos or other entitys that are within that dto. EX DTO that Convert perfectly for 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 for 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;
Would anyone have any idea how to implement this method or have done something similar.
NOTE: I’m testing if I can do what I want using apache Beanutils.
You’re trying to clone objects, right? To make a "deep cloning" (deep clone), you can serialize and deserialize an object or you can use a library specializing in cloning. See: http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/SerializationUtils.html and http://code.google.com/p/cloning/. I don’t understand where "Entity" and "DTO" come into this story - see that an untapped enitity can eventually be considered a DTO. If you explain your goal maybe we can point out simpler solution.
– Caffé
@Caffé Here in the company we have the Entity layer and Dto and we use Dto to carry the information, and in the service we convert to dto~Entity to work with the base and Entity~dto to carry with the information, We do so because we use many fields in dtos that are naughty in Entity. This method of work is already an old way of working here, only now they want to speed up the conversion of objects, the method I did above makes a part but not for the objects inside them. I looked at the apache but he also does not answer, he does what I did above.
– João
Doesn’t do what you do because he makes one deep Cloning (as long as all classes are serializable) while you are making a Shallow Cloning. Anyway the most suitable is the cloning more performative that I indicated in the second link, since it does not require the classes to be serialized and since serialize and deserialize is very expensive.
– Caffé