Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare proporder over extended class

Lets say i have class

@XmlType(propOrder = {
        "one",
        "two"
    })

@XmlRootElement(name = "search")
public class Search {

    protected One one;  
    protected Two two;

    //getters & setters
}

and i wanted a class that extends this class

e.g.

@XmlType(propOrder = {
        "three"
    })

@XmlRootElement(name = "searchExtended")
public class SearchExtended extends Search {

    protected Three three;  
    //getters & setters
}

how do you declare the propOrder correctly, i have tried this before and it did not use the order i thought it would. How is this handled by annotations? / how should you be declaring this across extended classes?

like image 897
Sean F Avatar asked Sep 18 '12 06:09

Sean F


1 Answers

The parents properties will be marshalled based on their specified ordering before the child properties. You can include the properties from the parent class in the propOrder of the child class if you annotate the parent class with @XmlTransient.

  • http://blog.bdoughan.com/2012/08/jaxbs-xmltransient-and-property-order.html

UPDATE

Is there a way i can make it transistant but still use it normally?

No, setting @XmlTransient on a class removes it from the classes that JAXB considers mapped. The reason that JAXB marhals the properties of the super class before the properties of the subclass is to match the rules of XML schema. When your Search class is not marked with @XmlTransient the corresponding XML schema is the following. According to the XML schema rules in order for a element of type searchExtended to be valid the elements from the super type must occur before any elements defined in the subtype.

  <xs:complexType name="searchExtended">
    <xs:complexContent>
      <xs:extension base="search">
        <xs:sequence>
          <xs:element name="three" type="three" minOccurs="0"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="search">
    <xs:sequence>
      <xs:element name="one" type="one" minOccurs="0"/>
      <xs:element name="two" type="two" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

You can see the XML schema that corresponds to your JAXB model by running the following code:

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SearchExtended.class);
        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceUri,
                    String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}
like image 188
bdoughan Avatar answered Nov 07 '22 19:11

bdoughan