Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex Java bean mapping

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:

  • Source.name => DestDTO.name and Source.desc => DestDTO.desc, okay easy.
  • Group Details instances in GenericDetailsDTO instances according to their code. For example, Details instances having "a" as code are all gathered into a single GenericDetailsDTO instance having "a" as code.
  • Each instance of Details is then mapped as item of the list details of the appropriate GenericDetailsDTO instance. This mapping is also complex:
    • Details.type => DetailsDTO.type, okay easy also.
    • For each "Attribute" of the attributes set of the Details instance, map the "value" field, with the correct type (String, Boolean or whatever), to the field of the DetailsDTO instance having as name the value of the field "name" of the same "Attribute" instance.

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.

like image 668
ObliTrabz Avatar asked Aug 10 '26 21:08

ObliTrabz


1 Answers

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.

like image 184
Alessandro Avatar answered Aug 13 '26 10:08

Alessandro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!