2016-06-13 355 views
5

我創建如下的映射。如何將平面dto對象屬性(如街道,城市等)映射到域對象中的嵌套地址。當我嘗試,我得到了一個錯誤:使用映射映射嵌套對象

[ERROR] diagnostic: Unknown property "address.postalCode" in return type. @Mapping(source = "city", target = "address.city"),

@Mapper(componentModel = "spring", uses = {}) 
public interface CompanyMapper { 
    @Mappings({ 
      @Mapping(source = "id", target = "id"), 
      @Mapping(source = "street", target = "address.street"), 
      @Mapping(source = "city", target = "address.city"), 
      @Mapping(source = "postalCode", target = "address.postalCode"), 
      @Mapping(source = "province", target = "address.province"), 
    }) 
    DomainObject map(DtoObject dto); 

和類...

public class Address { 
      private String street; 
      private Integer streetNumber; 
      private String city; 
      private String postalCode; 
      private String province; 
      //getters and setters 
    } 
public class DomainObject { 
     private String id; 
     private Address address; 
     //getters and setters 
} 

public class DtoObject { 
     private String id; 
     private String street; 
     private String city; 
     private String postalCode; 
     private String province; 
     //getters and setters 
} 

回答

4

嵌套在目標身邊,你要使用它還不支持。有一個功能請求(issue #389),但我們還沒有實現這一點。

+0

那麼有沒有其他解決方案來解決這個問題? 我認爲如果表達式在源代碼中會起作用! – TinyOS

+0

@Gunnar在目標端嵌套仍不支持?如果是的話,那麼這個「目標側嵌套」是由龍箱支持的嗎? –

0

我找不到在一種方法中做到這一點的方法。這裏是我的解決方案:

@Mapper 
public interface DtoObjectMapper { 

    Address toAddress(DtoObject dtoObject); 

    DomainObject toDomainObject(DtoObject dtoObject, Address address); 

} 

while using;

@Component 
public class SomeClass { 

    @Autowired 
    private DtoObjectMapper dtoObjectMapper; 

    public DomainObject convert(DtoObject dtoObject) { 
     return dtoObjectMapper.toDomainObject(dtoObject, dtoObjectMapper.toAddress(dtoObject)); 
    } 
}