2014-11-11 4 views
3

"a"개체가 "b"개체에 매핑되면 단방향 필드 만 매핑됩니다. "b"가 "a"로 매핑되면 해당 필드는 매핑되지 않습니다.Dozer 매핑 필드 유형 = "단방향"이 예상대로 작동하지 않습니다.

아래의 코드에서 "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 

나는 도저 5.4.0 버전을 사용하고 있습니다.

답변

0

나는 아래 코드를 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