【问题标题】:Dozer Mapping field type="one-way" not working as expected推土机映射字段类型 =“单向”未按预期工作
【发布时间】:2014-11-11 11:03:46
【问题描述】:

根据 Dozer 文档,单向字段仅在“a”对象映射到“b”对象时才被映射。如果“b”映射到“a”,则该字段未映射。

但是对于下面的代码,“b”仍然被映射到“a”。

 <mapping >
    <class-a>com.examples.source.Source</class-a>
    <class-b>com.examples.destination.Destination</class-b>
    <field type="one-way">
        <a set-method="setIRCCode" get-method="getIRCCode">ircCode</a>
        <b set-method="setIrcCode" get-method="getIrcCode">ircCode</b>
    </field> 
</mapping>

package com.examples.source;
public class Source {
    protected String ircCode;

    public String getIRCCode() {
        return ircCode;
    }
    public void setIRCCode(String value) {
        this.ircCode = value;
    }
}

package com.examples.destination;
public class Destination {
    private String ircCode;
    public String getIrcCode() {
        return this.ircCode;
    }
    public void setIrcCode(String ircCode) {
        this.ircCode = ircCode;
    }
}

public class Mapping {
    public static void main(String[] args) {
      DozerBeanMapper mapper = new DozerBeanMapper(Arrays.asList(new String[]{"Dozer-Mapping.xml"}));
      Destination destinationObj=new Destination();
      destinationObj.setIrcCode("B");
      Source srcObj= mapper.map(destinationObj, Source.class);
      System.out.println("Reverse Mapping IRCCode= "+ srcObj.getIRCCode());
   }
}

运行上述代码时的输出:

Reverse Mapping IRCCode= B **(Unexpected and Wrong Result)**

预期结果是:

Reverse Mapping IRCCode= null

我使用的是 Dozer 5.4.0 版本。

【问题讨论】:

    标签: java mapping pojo dozer apache-commons-beanutils


    【解决方案1】:

    我在 Dozer 5.5.1 中尝试了与您的帖子类似的以下代码,它提供的预期结果为 null

    代码:

    public class A {
    
        private String aName;
        private String aPlace;
        /**
         * @return the aName
         */
        public String getaName() {
            return aName;
        }
        /**
         * @param aName the aName to set
         */
        public void setaName(String aName) {
            this.aName = aName;
        }
        /**
         * @return the aPlace
         */
        public String getaPlace() {
            return aPlace;
        }
        /**
         * @param aPlace the aPlace to set
         */
        public void setaPlace(String aPlace) {
            this.aPlace = aPlace;
        }
    
    }
    
    public class B {
        private String bName;
        private String bPlace;
        /**
         * @return the bName
         */
        public String getbName() {
            return bName;
        }
        /**
         * @param bName the bName to set
         */
        public void setbName(String bName) {
            this.bName = bName;
        }
        /**
         * @return the bPlace
         */
        public String getbPlace() {
            return bPlace;
        }
        /**
         * @param bPlace the bPlace to set
         */
        public void setbPlace(String bPlace) {
            this.bPlace = bPlace;
        }
    }
    

    推土机映射:

      <mapping> 
        <class-a>com.test.A</class-a>
        <class-b>com.test.B</class-b>   
        <field type="one-way">
          <a>aName</a>
          <b>bName</b>
        </field>
      </mapping>  
    

    测试:

    public static void main(String[] arg){
        List<String> myMappingFiles = new ArrayList<String>(1);
        myMappingFiles.add("dozer-mapping.xml");
    
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.setMappingFiles(myMappingFiles);
        B obj=new B();
        obj.setbName("Test");
        A destObject = 
            mapper.map(obj, A.class);
                  System.out.println("B="+destObject.getaName());
    }
    

    输出:


    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    B=null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-04
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多