简体   繁体   English

推土机:是否可以在所有映射的类中单向排除字段?

[英]Dozer: Is it possible to one-way exclude a field on all mapped classes?

I want to exclude all 'createdDate' fields from being deserialized on all of my mapped fields: 我想从所有映射字段上反序列化的过程中排除所有“ createdDate”字段:

Similar to this: 与此类似:

<field-exclude type="one-way""> <a>createdDate</a> <b>createdDate</b> </field-exclude>

Thus, having the field visible when querying but not updatable. 因此,查询时该字段可见,但不可更新。

Is there a way to do this globally without having to specify it on each and every mapping? 有没有一种全局方法可以不必在每个映射上都指定它?

Move all your common fields to super class on source and destination classes. 将所有公共字段移至源类和目标类的超类。 and create mapping for super classes. 并为超级类创建映射。 Full example can be found at Dozer fields - Global Exclude Example 完整示例可在推土机字段中找到-全局排除示例

Step 1:Source POJOs: 步骤1:来源POJO:

    //PersonType.java
public class AddressType extends BaseType{

    private String addrLine1;
    private String city;
    private String state;
    private int zipCode;
    // Setter and Getter methods are removed
}
// BaseType.java
public class BaseType { 
        private String createdDate;
    // Setter and Getter methods are removed
}

Step 2: Target POJOs: 步骤2:定位POJO:

// Address.java
public class Address extends BaseDomain{

    private String addrLine1;
    private String city;
    private String state;
    private int zip5;
// Setter and Getter methods are removed
}
//BaseDomain.java
public class BaseDomain {   
    private String createdDate;
// Setter and Getter methods are removed
}

Step 3: Create Mapping 步骤3:建立对应

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
     <mapping>
            <class-a>com.codesimplify.dozersamples.fieldexclude.AddressType</class-a>
            <class-b>com.codesimplify.dozersamples.fieldexclude.Address</class-b>   
    </mapping>

   <mapping>
    <class-a>com.codesimplify.dozersamples.fieldexclude.BaseType</class-a>
    <class-b>com.codesimplify.dozersamples.fieldexclude.BaseDomain</class-b>
    <field-exclude type="one-way">
        <a>createdDate</a>
        <b>createdDate</b>
    </field-exclude>    
    </mapping>
</mappings>

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

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