Anchit Pancholi
Anchit Pancholi

Reputation: 1214

MapStruct error with Nested Source and Nested Target Properties

I am using mapstruct for converting Domain in DTO below is mapping is am using :-

   @Mapper(componentModel = "spring", uses = {})
   public interface CategoriesMapper {
      @Mapping(source = "regionsid.id", target = "regionsid.id")
      CategoriesDTO categoriesToCategoriesDTO(Categories categories);  
   } 

    public class Categories implements Serializable {
        private Regions regionsid;
        public Regions getRegionsid() {
            return regionsid;
        }

        public void setRegionsid(Regions regions) {
            this.regionsid = regions;
        }

    }

    public class CategoriesDTO implements Serializable {
        private RegionsDTO regionsid;
        public RegionsDTO getRegionsid() {
            return regionsid;
        }

        public void setRegionsid(RegionsDTO regions) {
            this.regionsid = regions;
        }

    }

    public class Regions implements Serializable {
        private Long id;
    }

    public class RegionsDTO implements Serializable {
        private Long id;
    }

So i have some structure at both source and target side, but i am getting below error:-

CategoriesMapper.java:28: error: Unknown property "regionsid.id" in return type.

what if i have Set

Source -

private Set<Regions> regions = new HashSet<>();

Target -

private Set<RegionsDTO> regions = new HashSet<>();

CategoriesMapper.java:36: error: Can't map property "java.util.Set<com.equidity.xboard.service.dto.RegionsDTO> regions" to "java.util.Set<com.equidity.xboard.domain.Regions> regions". Consider to declare/implement a mapping method: "java.util.Set<com.equidity.xboard.domain.Regions> map(java.util.Set<com.equidity.xboard.service.dto.RegionsDTO> value)".`

Upvotes: 0

Views: 2541

Answers (2)

Adam Wise
Adam Wise

Reputation: 2290

The cleanest way to do this I believe is to write a RegionsMapper interface, something like the following:

@Mapper(componentModel = "spring", uses = {})
public interface RegionsMapper extends EntityMapper<RegionsDTO, Regions> {

    Set<Regions> map(Set<RegionsDTO> value);
}

Note that the above code is declaring the method that the error message is asking for.

In addition you will also need to tweak the @Mapper annotation (first line of code in the question) so that it uses this new mapper:

@Mapper(componentModel = "spring", uses = {RegionsMapper})

This question is a little old, but I just ran into something very much like it using JHipster.

Upvotes: 0

Filip
Filip

Reputation: 21423

Currently MapStruct does not support automatic mapping of nested properties. In order for your mapping to work you will need to provide one more method to your CategoriesMapper:

RegionsDTO regionsToRegionsDTO(Regions Regions); 

There is currently on open issue (#60) and a pending PR (#937) that are looking to tackle this new feature.

Upvotes: 0

Related Questions