Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB inheritance conflict - Re-annotating on sub-classes
    primarykey
    data
    text
    <p>I currently have this environment on my project:</p> <pre><code>public abstract class Foo { private List&lt;Thing&gt; things; public List&lt;Thing&gt; getThings() { return this.things; } } public abstract class Bar extends Foo { @XmlElements({@XmlElement(name = "first", type = First.class)}) public List&lt;Thing&gt; getThings() { return super.getThings(); } } public class Bobar extends Bar { @XmlElements({@XmlElement(name = "second", type = Second.class)}) public List&lt;Thing&gt; getThings() { return super.getThings(); } } </code></pre> <p>For the following XML document</p> <pre><code>&lt;bobar&gt; &lt;first&gt;blablabla&lt;/first&gt; &lt;second&gt;blublublu&lt;/second&gt; &lt;/bobar&gt; </code></pre> <p>When I do</p> <pre><code>context = JAXBContext.newInstance("the.package.structure"); unmarshaller = context.createUnmarshaller(); Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document"); </code></pre> <p>The Bar <code>object</code> only has one element in the collection, not 2. The <code>First</code> element is completly lost, when I try to do <code>object.getThings()</code>, its size is 1 and the only object inside the collection is an instance of <code>Second</code>. Can someone help me how can I achieve to get both objects in the collection? And if that's not possible, how can I achieve something similar to this?</p> <p>The reason I'm doing this is that (in my project logic) every <code>Bobar</code>s things collection has a <code>First</code> in its collection, but not every <code>Bar</code> has a <code>Second</code> in its collection, and <code>Foo</code> is a generic class.</p> <h2>Edit:</h2> <p>When I change the order in my XML document, the output is different.</p> <pre><code>&lt;bobar&gt; &lt;second&gt;blablabla&lt;/second&gt; &lt;first&gt;blublublu&lt;/first&gt; &lt;/bobar&gt; </code></pre> <p>In this scenario, I get only an instance of <code>First</code> in the collection, and <code>Second</code> is lost. And changing the scenario more, I get interesting results:</p> <pre><code>public abstract class Foo { private List&lt;Thing&gt; things; public List&lt;Thing&gt; getThings() { return this.things; } } public abstract class Bar extends Foo { @XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)}) public List&lt;Thing&gt; getThings() { return super.getThings(); } } public class Bobar extends Bar { @XmlElements({@XmlElement(name = "second", type = Second.class)}) public List&lt;Thing&gt; getThings() { return super.getThings(); } } </code></pre> <p>If I do</p> <pre><code>&lt;bobar&gt; &lt;third&gt;bliblibli&lt;/third&gt; &lt;second&gt;blablabla&lt;/second&gt; &lt;first&gt;blublublu&lt;/first&gt; &lt;/bobar&gt; </code></pre> <p>In theory, I think this shouldn't be validated against the XML Schema generated by that, as the order here is not correct. But besides that, in such scenario, I get <code>Second</code> and <code>First</code>, the <code>Third</code> is lost.</p>
    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. 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