Reputation: 31
I need to find a solution to map entities to DTOs.
I know there is a lot of libraries for that but the conversion I need to do is quite complex and I have not been able to find a satisfactory solution.
The entities I have look like this:
public class Source {
private String name;
private String desc;
private Set<Details> details = new HashSet<Details>();
}
public class Details {
private String code;
private String type;
private Set<Attribute> attributes = new HashSet<Attribute>();
}
public class Attribute {
private String name;
private String value;
}
For the sake of clarity, I removed persistence annotations and getters/setters and omitted some fields.
And the DTOs:
public class DestDTO {
private String name;
private String desc;
private List<GenericDetailsDTO> genericDetails;
}
public class GenericDetailsDTO {
private String code;
private List<DetailsDTO> details;
}
public class DetailsDTO {
private String type;
private String field1;
private Boolean field2;
private Integer field3;
}
The mapping I want to do is as follow:
I tried JMapper but I have not been able to implement such a solution.
I have been able to do it by using Dozer but I would like to avoid this library because of its slow performances (According to various articles on the net such as http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/).
In a perfect world, I would also like to use as much as possible annotation/api configuration and avoid XML configuration. And if the mapper instance could also be injected as bean through Spring API (Not XML once again), it would really be a wonderful world.
So my question is: would you have any advises or could you please recommend me a library for this issue?
Thanks for your help.
PS: Sorry if it is not very clear, but it is quite hard to explain in few words. Please, do not hesitate to ask more explanation.
Upvotes: 3
Views: 2214
Reputation: 282
i haven't understood very well what do you mean, but i try to give you a solution with JMapper.
Source fields:
@JMap private String name;
@JMap private String desc;
@JMap("genericDetails")
private Set<Details> details = new HashSet<Details>();
Details fields:
@JMap private String code;
@JMap("details")
private String type;
@JMap("details")
private Set<Attribute> attributes = new HashSet<Attribute>();
@JMapConversion(from="type",to="details",type=Type.STATIC)
public static List<DetailsDTO> typeMap(List<DetailsDTO> destination, String sourceType){
for (DetailsDTO detailsDTO : destination)
detailsDTO.setType(sourceType);
return destination;
}
@JMapConversion(from="attributes",to="details",type=Type.STATIC)
public static List<DetailsDTO> detailsMap(List<DetailsDTO> destination, Set<Attribute> attributes){
// ??
return destination;
}
Where "??" is for what I did not understood.
Upvotes: 1