简体   繁体   English

不带setter的对象的推土机映射

[英]Dozer mapping for objects without setters

public class ParentOne {

    private List<ChildOne> child;

    public List<ChildOne> getChild() {
        if (child == null) {
            child = new ArrayList<ChildOne>();
        }
        return child;
    }
}

public class ParentTwo {

    private List<ChildTwo> child;

    public List<ChildTwo> getChild() {
        if (child == null) {
            child = new ArrayList<ChildTwo>();
        }
        return child;
    }
}

DOZER Mapping DOZER映射

<mapping>
    <class-a>ParentOne</class-a>
    <class-b>ParentTwo</class-b>
    <field>
        <a is-accessible="true">child</a>
        <b is-accessible="true">child</b>
    </field>
</mapping>

public class Client {

    public static void main(String[] args) {
        ParentOne parentOne = new ParentOne();
        parentOne.getChild().add(new ChildOne());
        parentOne.getChild().add(new ChildOne());

        ParentTwo parentTwo = dozerBeanMapper.map(parentOne, ParentTwo.class);
    }
}

The Parent class doesn't contain the setters for the list. Parent类不包含列表的setter。 hence I made the dozer mapping in such a way to access the fields directly. 因此,我以直接访问字段的方式制作了推土机映射。 The mapping happens fine but the problem is after mapping the parentTwo instance contains child of type ChildOne, it is supposed to be ChildTwo. 映射进行得很好,但是问题是映射parentTwo实例包含类型为ChildOne的子对象之后,应该是ChildTwo。

Did I miss anything? 我有想念吗?

Add hints (see here for more info). 添加提示(有关更多信息,请参见此处 )。

<mapping>
    <class-a>ParentOne</class-a>
    <class-b>ParentTwo</class-b>
    <field>
        <a is-accessible="true">child</a>
        <b is-accessible="true">child</b>
        <a-hint>ChildOne</a-hint>
        <b-hint>ChildTwo</b-hint>
    </field>
</mapping>

You might need to use the fully qualified names of the classes rather than just ChildOne and ChildTwo , but other than that, it should work. 您可能需要使用类的完全限定名称,而不仅仅是ChildOneChildTwo ,但ChildTwo ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM