2010-10-19 61 views
2

我有一個簡單的類CustomQuoteRequest:映射一個Java屬性設置幾個XML屬性使用JAXB和莫西

public class CustomQuoteRequest { 

    private String requestId; 

    private String currencyPairCode; 

    public String getRequestId() { 
    return requestId; 
    } 

    public void setRequestId(String requestId) { 
    this.requestId = requestId; 
    } 

    public String getCurrencyPairCode() { 
    return currencyPairCode; 
    } 

    public void setCurrencyPairCode(String currencyPairCode) { 
    this.currencyPairCode = currencyPairCode; 
    } 
} 

我想currencyPairCode映射到XML兩種不同的屬性。這是我使用的莫西映射文件:

<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd" 
    > 
    <java-types> 
     <java-type name="com.anz.fxeasy.domain.model.quote.CustomQuoteRequest" xml-accessor-type="FIELD"> 
      <xml-root-element name="FIXML"/> 
      <java-attributes> 
       <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/> 
       <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"></xml-element> 
       <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Leg/Leg/@Sym"></xml-element> 
      </java-attributes> 
     </java-type> 
    </java-types> 

但是第二個XML元素似乎覆蓋前一個。有任何想法嗎? 非常感謝

回答

2

的EclipseLink莫西的2.1.x

在EclipseLink的2.1.x的,你可以使用XML定製,以實現這一點。您的外部元數據如下所示:

<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd" 
    > 
    <java-types> 
     <java-type name="forum78.CustomQuoteRequest" xml-accessor-type="FIELD" xml-customizer="customizer.CustomQuoteRequestCustomizer"> 
      <xml-root-element name="FIXML"/> 
      <java-attributes> 
       <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/> 
       <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

在定製器中,我們將爲currencyCodePair屬性添加第二個映射。我們需要指出這個映射是隻寫的。在XML定製的實施將如下所示:

package customizer; 

import org.eclipse.persistence.config.DescriptorCustomizer; 
import org.eclipse.persistence.descriptors.ClassDescriptor; 
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping; 

public class CustomQuoteRequestCustomizer implements DescriptorCustomizer { 

    public void customize(ClassDescriptor descriptor) throws Exception { 
     XMLDirectMapping currencyPairCodeLegMapping = new XMLDirectMapping(); 
     currencyPairCodeLegMapping.setAttributeName("currencyPairCode"); 
     currencyPairCodeLegMapping.setXPath("QuotReq/QuoteReq/Leg/Leg/@Sym"); 
     currencyPairCodeLegMapping.setIsWriteOnly(true); 
     descriptor.addMapping(currencyPairCodeLegMapping); 

    } 

} 

的EclipseLink莫西2.2

在即將到來的EclipseLink 2.2發行版,你將能夠做到這一點只用外化的元數據:

<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd" 
    > 
    <java-types> 
     <java-type name="forum78.CustomQuoteRequest" xml-accessor-type="FIELD"> 
      <xml-root-element name="FIXML"/> 
      <java-attributes> 
       <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/> 
       <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"/> 
       <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Leg/Leg/@Sym" write-only="true"/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

以下錯誤可用於追蹤此支持:

+0

太棒了,非常感謝! – Sergio 2010-10-19 20:21:06