2016-02-29 37 views
4

給予相同的一組四個對象:Mapstruct:映射多個源對象子對象

A{String one, B b} 

B{String two, String three} 

C{String one, String two} 

D{String three} 

我想產生這樣的映射:

A cAndDToA(C c , D d); 

我目前還不能找到一種方法來填充B中的對象與來自C和D的數據。

有沒有人知道這個問題的解決方案,或有更好的方法?

回答

3

你可以定義一個方法用於填充從CBD

B cAndDToB(C c, D d); 

,然後通過手動調用這個decoratorcAndDToA

@Mapper(decoratedWith=MyMapperDecorator.class) 
public interface MyMapper { 
    A cAndDToA(C c, D d); 
    B cAndDToB(C c, D d); 
} 

public abstract class MyMapperDecorator implements MyMapper { 

    private final MyMapper delegate; 

    public MyMapperDecorator(PersonMapper delegate) { 
     this.delegate = delegate; 
    } 

    @Override 
    public A cAndDToA(C c, D d) { 
     A a = delegate.cAndDToA(c, d); 
     a.setB(cAndDToB(c, d); 

     return a; 
    } 
} 

我們將提供對nested mappings支持目標方也是如此。但我們還沒有:)

+0

工作就像一個魅力,感謝您的建議。到目前爲止,我對這個庫印象非常深刻,並期待着這些嵌套映射的實現。 – Ghen