I would, in the scenario below, like the binding of java-type name="SubClass"
to be applied to set the text field on SuperClass. However it is not. Is there a problem with overriding the bindingsA.xml?
According to the Overriding rules documentation:
If the same java-type occurs in multiple files, any values that are set in the later file, will override values from the previous file
What do I need to do to make it work?
Input:
<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>
Bindings 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>
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>
Classes:
public class SuperClass {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
public class SubClass extends SuperClass { }
Demo:
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());
Output:
[EL Warning]: 2013-07-31 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text
It's a bit odd to map the
text
property differently on the super and sub classes. If this is something you really want to do, then below is a way you could accomplish this.Java Model
SuperClass
SubClass
We will override the accessor methods from the super class. This will help us trick MOXy into thinking that
SubClass
has its own propertytext
.Metadata
bindings.xml
In the mapping document we will tell MOXy that the real super class of
SubClass
isjava.lang.Object
.Demo Code
Below is some demo code you can run to prove that everything works:
Demo
Output