EclipseLink MOXy:覆盖绑定文件的规则

时间:2013-07-31 23:19:30

标签: inheritance jaxb eclipselink override moxy

在下面的场景中,我会像java-type name="SubClass"的绑定一样应用于在SuperClass上设置文本字段。但事实并非如此。覆盖bindingsA.xml有问题吗? 根据{{​​3}}:

  

如果多个文件中出现相同的java类型,则在以后的文件中设置的任何值都将覆盖上一个文件中的值

我需要做些什么才能让它发挥作用?

输入:

<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>

绑定A:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

 <java-types>        
   <java-type name="SuperClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="@text" />
    </java-attributes>
  </java-type>
 </java-types>

</xml-bindings>

绑定B:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

<java-types>        
  <java-type name="SuperClass" xml-transient="true"></java-type>
  <java-type name="SubClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="text()" />
    </java-attributes>
  </java-type>
</java-types>

</xml-bindings>

类:

public class SuperClass {

 private String text;

 public String getText() {
    return text;
 }

 public void setText(String text) {
    this.text = text;
 }
}

public class SubClass extends SuperClass { }

演示:

Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
List<String> bindings = new LinkedList<String>();
bindings.add("bindingsA.xml");
bindings.add("bindingsB.xml");
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(superClass.getText()); 

输出:

[EL Warning]: 2013-07-31 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text

1 个答案:

答案 0 :(得分:1)

在超类和子类上以不同方式映射text属性有点奇怪。如果这是你真正想做的事情,那么下面就是你可以做到这一点的方法。

Java模型

<强>父类

package forum17982654;

public class SuperClass {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

<强>子类

我们将覆盖超类中的访问器方法。这将有助于我们让MOXy认为SubClass有自己的属性text

package forum17982654;

public class SubClass extends SuperClass {

    @Override
    public String getText() {
        return super.getText();
    }

    @Override
    public void setText(String text) {
        super.setText(text);
    } 

}

元数据

<强> bindings.xml

在映射文档中,我们将告诉MOXy SubClass的真正超类是java.lang.Object

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17982654">
    <java-types>
        <java-type name="SuperClass">
            <xml-root-element/>
            <java-attributes>
                <xml-attribute java-attribute="text"/>
            </java-attributes>
        </java-type>
        <java-type name="SubClass" super-type="java.lang.Object">
            <xml-root-element/>
            <java-attributes>
                <xml-value java-attribute="text"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示代码

下面是一些可以运行的演示代码,以证明一切正常:

<强>演示

package forum17982654;

import java.io.StringReader;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
        jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader superClassXML = new StringReader("<superClass text='Hello Super Class'/>");
        SuperClass superClass = (SuperClass) unmarshaller.unmarshal(superClassXML);
        System.out.println(superClass.getText());

        StringReader subClassXML = new StringReader("<subClass>Hello Sub Class</subClass>");
        SubClass subClass = (SubClass) unmarshaller.unmarshal(subClassXML);
        System.out.println(subClass.getText());
    }

}

<强>输出

Hello Super Class
Hello Sub Class