【问题标题】:Dozer: Is it possible to one-way exclude a field on all mapped classes?推土机:是否可以单向排除所有映射类上的字段?
【发布时间】:2016-06-21 21:03:12
【问题描述】:

我想在我的所有映射字段上排除所有“createdDate”字段被反序列化:

与此类似:

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

因此,该字段在查询时可见但不可更新。

有没有办法在全局范围内执行此操作而不必在每个映射上都指定它?

【问题讨论】:

    标签: java jpa dozer


    【解决方案1】:

    将所有常用字段移至源类和目标类的超类。并为超类创建映射。完整的例子可以在Dozer fields - Global Exclude Example找到。

    第 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
    }
    

    第 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
    }
    

    第 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多