Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the log message you posted, I can see that you are using MOXy's external mapping document (see <a href="http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html" rel="nofollow">http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html</a>). There are a couple of different ways to map the inherited property.</p> <hr/> <p><strong>OPTION #1 - Map the Inherited Property Belonging to the Parent</strong></p> <p>By default a field/property needs to be mapped within the class it belongs to. Since MOXy scopes the external mapping document at the package level, you will need separate mapping documents for <code>A</code> and <code>B</code>. </p> <p><strong>forum10874711/a/binding1.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.a"&gt; &lt;java-types&gt; &lt;java-type name="A"&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>forum10874711/b/binding1.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.b"&gt; &lt;java-types&gt; &lt;java-type name="B"&gt; &lt;xml-root-element/&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>forum10874711/b/jaxb.properties</strong></p> <p>To specify MOXy as your JAXB implementation you need to add a file called <code>jaxb.properties</code> in the same package as your domain model with the following entry.</p> <pre><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory </code></pre> <p><strong>Demo</strong></p> <pre><code>package forum10874711; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextFactory; import forum10874711.b.B; public class Demo1 { public static void main(String[] args) throws Exception { Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(1); List&lt;String&gt; metadata = new ArrayList&lt;String&gt;(2); metadata.add("forum10874711/a/binding1.xml"); metadata.add("forum10874711/b/binding1.xml"); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata); JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties); B b = new B(); b.setFieldOfClassA("foo"); b.setFieldOfClassB(123); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(b, System.out); } } </code></pre> <p><strong>Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;b&gt; &lt;field-of-class-a&gt;foo&lt;/field-of-class-a&gt; &lt;field-of-class-b&gt;123&lt;/field-of-class-b&gt; &lt;/b&gt; </code></pre> <hr/> <p><strong>OPTION #2 - Map the Inherited Property Belonging to Child</strong></p> <p>The parent class <code>A' can be marked</code>@XmlTransient<code>this will allow us to map the inherited fields/properties on the child class</code>B` (see <a href="http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html" rel="nofollow">http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html</a>).</p> <p><strong>forum10874711/a/binding2.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.a"&gt; &lt;java-types&gt; &lt;java-type name="A" xml-transient="true"/&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>forum10874711/b/binding2.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.b"&gt; &lt;java-types&gt; &lt;java-type name="B"&gt; &lt;xml-root-element/&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/&gt; &lt;xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>Demo</strong></p> <pre><code>package forum10874711; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextFactory; import forum10874711.b.B; public class Demo2 { public static void main(String[] args) throws Exception { Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(1); List&lt;String&gt; metadata = new ArrayList&lt;String&gt;(2); metadata.add("forum10874711/a/binding2.xml"); metadata.add("forum10874711/b/binding2.xml"); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata); JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties); B b = new B(); b.setFieldOfClassA("foo"); b.setFieldOfClassB(123); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(b, System.out); } } </code></pre> <p><strong>Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;b&gt; &lt;field-of-class-a&gt;foo&lt;/field-of-class-a&gt; &lt;field-of-class-b&gt;123&lt;/field-of-class-b&gt; &lt;/b&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload