Aditya Sirohi
Aditya Sirohi

Reputation: 75

Mapstruct : Map Object in List to Object

I am trying to map Entity object into DTO to transfer to the frontend app. Query returns List which contains institutionName and internalName and i want to map them to List of modelsInstitution and modelsInternalName in DTO. I am stuck with my mappers class.

Below is my Entity:

@Entity
@DynamoDBTable(tableName = "Inventory")
public class Inventory 
{
    @Id
    private String id;
    private String institutionName;
    private String internalName;
    private String lanAdapter;

    public Inventory() {
    }

    public Inventory(String Id,String InstitutionName, String LanAdapter, String InternalName) 
    {
        this.id = Id
        this.lanAdapter = LanAdapter;
        this.institutionName = InstitutionName;
        this.internalName = InternalName;

    }

    @DynamoDBHashKey(attributeName = "Id")
    public String getId() 
    {
        return id;
    }
    public void setId(String id) 
    {
        this.id=id;
    }

    @DynamoDBAttribute(attributeName = "InstitutionName")
    public String getInstitutionName()
    {
        return institutionName;
    }
    public void setInstitutionName(String InstitutionName)
    {
        this.institutionName = InstitutionName;
    }

    @DynamoDBAttribute(attributeName = "LanAdapter")
    public String getLanAdapter()
    {
        return lanAdapter;
    }
    public void setLanAdapter(String LanAdapter)
    {
        this.lanAdapter = LanAdapter;
    }

        @DynamoDBAttribute(attributeName = "InternalName")
    public String getInternalName()
    {
        return internalName;
    }
    public void setInternalName(String InternalName)
    {
        this.internalName = InternalName;
    }
}

Below is my DTO Object:

public class FilterDto 
{
    private List<String> modelsInstitution;
    private List<String> modelsInternalName;

    public FilterDto(List<String> modelsInstitution, List<String> versions) {
        super();
        this.modelsInstitution = modelsInstitution;
        this.modelsInternalName = modelsInternalName;
    }

    public List<String> getModelInstitution() {
        return modelsInstitution;
    }
    public void setModelInstitution(List<String> modelsInstitution) {
        this.modelsInstitution = modelsInstitution;
    }
    public List<String> getModelInternalName() {
        return modelsInternalName;
    }
    public void setModelInternalName(List<String> modelsInternalName) {
        this.modelsInternalName = modelsInternalName;
    }
}

Mapper Class:

My mapping is not correct, i am missing how to map Entity in List to DTO.

@IterableMapping(elementTargetType = Inventory.class)
@Mappings({
    @Mapping(target="modelsInstitution", expression="java(inventory.getInstitution())"),
    @Mapping(target="modelsInternalName", expression="java(inventory.getInternalName())")
})
FilterDto InventoryToFilterDTO1(List<Inventory> inventory);

Upvotes: 3

Views: 17446

Answers (1)

Filip
Filip

Reputation: 21403

You can't really do this cleanly with MapStruct. I would suggest you to do this mapping manually.

Your mapping can look like:

public interface MyMapper {

    default FilterDto InventoryToFilterDTO1(List<Inventory> inventory) {
        List<String> institutions = new ArrayList<>(inventory.size());
        List<String> names = new ArrayList<>(inventory.size());

        for(Inventory inv: inventory) {
            institutions.add(inv.getInstitution());
            names.add(inv.getInternalName()));
        }

        return new FilterDto(institutions, names);
    }
}

Theoretically, there might be a way to do this with MapStruct, by using @Context, @AfterMapping, @BeforeMapping and @ObjectFactory. However, this would be much more complex.

Upvotes: 4

Related Questions