inheritance - EclipseLink MOXy: Override rules of binding files -
i would, in scenario below, binding of java-type name="subclass" applied set text field on superclass. not. there problem overriding bindingsa.xml? according overriding rules documentation:
if same java-type occurs in multiple files, values set in later file, override values previous file
what need make 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] no property generated it. text
it's bit odd map text property differently on super , sub classes. if want do, below way accomplish this.
java model
superclass
package forum17982654; public class superclass { private string text; public string gettext() { return text; } public void settext(string text) { this.text = text; } } subclass
we override accessor methods super class. trick moxy thinking subclass has own property text.
package forum17982654; public class subclass extends superclass { @override public string gettext() { return super.gettext(); } @override public void settext(string text) { super.settext(text); } } metadata
bindings.xml
in mapping document tell moxy real super class of 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> demo code
below demo code can run prove works:
demo
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()); } } output
hello super class hello sub class
Comments
Post a Comment