Question

I ve a confusion regarding how the dozer maps the source object to destination object. I have the following scenario:

Source Object:

public class Rule {
private String id;
private String name;
private String group;
private String content;
private RuleType ruleType;
private String drlContent;
private boolean enabled;
private Strategy strategy;
// getters and setters
}

Destination Object:

public class RuleActivity {
private String id;
private String name;
private Strategy strategy;
// getters and setters
}

XML Mapping:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <stop-on-errors>true</stop-on-errors>
    <date-format>MM/dd/yyyy HH:mm</date-format>
</configuration>

<mapping wildcard="false">
    <class-a>com.magick.models.shared.Rule</class-a>
    <class-b>com.magick.models.shared.log.RuleActivity</class-b>
    <field>
        <a>id</a>
        <b>ruleId</b>
    </field>
    <field>
        <a>strategy.name</a>
        <b>strategy.name</b>
    </field>
     <field>
        <a>name</a>
        <b>name</b>
    </field>
</mapping>

Now How these would be mapped ? I mean , does the destination Object contains the Complete Strategy Object or only the strategy.name field of it.

Was it helpful?

Solution

First of all, by default dozer mappings are bi-directional. So, mapping from class-a to class-b and vice-versa is permitted.

As you have done your mapping as follows:

<field>
    <a>strategy.name</a>
    <b>strategy.name</b>
</field>

If the source object is having a Strategy object which is not null and have all the relevant field's value. Then dozer will create a new Strategy object for destination as well and will only populate the name field of newly created Strategy object.

Further, dozer also works on retrospection so suppose the name and type of all fields of Strategy object in source and destination is same. Dozer will map or copy all the fields automatically. So you don't have to map each field individually. you just have to write as below.

<field>
    <a>strategy</a>
    <b>strategy</b>
</field>

But if your field names or type is not same, you need to define mapping for each field as you did for id field for Rule class and ruleId field for RuleActivity class.

<field>
    <a>id</a>
    <b>ruleId</b>
</field>

Hope this clarifies your doubts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top