简体   繁体   English

推土机测绘混乱

[英]Dozer Mapping Confusion

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映射:

<?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. 我的意思是,目标对象包含完整的策略对象还是仅包含它的strategy.name字段。

First of all, by default dozer mappings are bi-directional. 首先,默认情况下,推土机映射是双向的。 So, mapping from class-a to class-b and vice-versa is permitted. 因此,允许从class-a class-b映射到class-b ,反之亦然。

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. 如果源对象具有不为null且具有所有相关字段值的Strategy对象。 Then dozer will create a new Strategy object for destination as well and will only populate the name field of newly created Strategy object. 然后推土机还将为目的地创建一个新的Strategy对象,并且只会填充新创建的Strategy对象的name字段。

Further, dozer also works on retrospection so suppose the name and type of all fields of Strategy object in source and destination is same. 此外,推土机还可以进行追溯,因此假设源和目标中Strategy对象所有字段的名称和类型相同。 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. 但是如果你的字段名称或类型不一样,你需要定义为你做的每个字段映射id字段Rule类和ruleId领域RuleActivity类。

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

Hope this clarifies your doubts. 希望这可以澄清您的疑问。

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

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