Can dozer do this with inheritance/polymorphism mapping?

911 views
Skip to first unread message

agentdark64

unread,
Oct 9, 2014, 8:21:14 AM10/9/14
to dozer-...@googlegroups.com
Here's some example code to understand better:

DestinationObject.java
public class DestinationObject {

    private String testValOne;
    private String testValTwo;
    private DestObjTwo two;

    public String getTestValOne() {
        return testValOne;
    }

    public void setTestValOne(String testValOne) {
        this.testValOne = testValOne;
    }

    public String getTestValTwo() {
        return testValTwo;
    }

    public void setTestValTwo(String testValTwo) {
        this.testValTwo = testValTwo;
    }

    public DestObjTwo getTwo() {
        return two;
    }

    public void setTwo(DestObjTwo two) {
        this.two = two;
    }
}

DestObjTwo.java:

public class DestObjTwo {

    private String anotherValOne;

    public String getAnotherValOne() {
        return anotherValOne;
    }

    public void setAnotherValOne(String anotherValOne) {
        this.anotherValOne = anotherValOne;
    }
}

DestObjTwoSecondary.java (Notice it is extending DestObjTwo)
public class DestObjTwoSecondary extends DestObjTwo {

    private String bla;
    private String anotherValTwo;

    public String getBla() {
        return bla;
    }

    public void setBla(String bla) {
        this.bla = bla;
    }

    public String getAnotherValTwo() {
        return anotherValTwo;
    }

    public void setAnotherValTwo(String anotherValTwo) {
        this.anotherValTwo = anotherValTwo;
    }
}

SourceObject.java:
public class SourceObj {

    private String testValOne;
    private String testValTwo;
    private String anotherValOne;
    private String anotherValTwo;

    public String getTestValOne() {
        return testValOne;
    }

    public void setTestValOne(String testValOne) {
        this.testValOne = testValOne;
    }

    public String getAnotherValTwo() {
        return anotherValTwo;
    }

    public void setAnotherValTwo(String anotherValTwo) {
        this.anotherValTwo = anotherValTwo;
    }

    public String getAnotherValOne() {
        return anotherValOne;
    }

    public void setAnotherValOne(String anotherValOne) {
        this.anotherValOne = anotherValOne;
    }

    public String getTestValTwo() {
        return testValTwo;
    }

    public void setTestValTwo(String testValTwo) {
        this.testValTwo = testValTwo;
    }
}

I call the mapper like this:
        Mapper mapper = new DozerBeanMapper(Arrays.asList("dozerMapping.xml"));

        SourceObj sourceObject = new SourceObj();
        sourceObject.setAnotherValOne("anotherValOne");
        sourceObject.setAnotherValTwo("anotherValTwo");
        sourceObject.setTestValOne("testValOne");
        sourceObject.setTestValTwo("testValTwo");

        try {
            DestinationObject destObject =
                    mapper.map(sourceObject, DestinationObject.class);


My mapping looks like this:
<?xml version="1.0" encoding="UTF-8"?>
          xsi:schemaLocation="http://dozer.sourceforge.net

    <configuration>
        <stop-on-errors>true</stop-on-errors>
        <date-format>MM/dd/yyyy HH:mm</date-format>
        <wildcard>true</wildcard>
    </configuration>

    <mapping>
        <class-a>testDozer.account.SourceObj</class-a>
        <class-b>testDozer.account.DestinationObject</class-b>
        <field>
            <a>testValOne</a>
            <b>testValOne</b>
        </field>
        <field>
            <a>testValTwo</a>
            <b>testValTwo</b>
        </field>
        <field>
            <a>anotherValOne</a>
            <b>two.anotherValOne</b>
        </field>
        <field>
            <a>anotherValTwo</a>
            <b>two.anotherValTwo</b>
        </field>
    </mapping>
</mappings>

So basically, when it creates "two" in the mapping, it will create an object of type "DestObjTwoSecondary" even though DestinationObject references type "DestObjTwo". etc I want to be able to define in the mapping what the top level subclass is and to use that instead regardless of how it is defined in the code.




Reply all
Reply to author
Forward
0 new messages