Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can/Should I list inherited properties for a JAXB mapped bean in the "propOrder" annotation?

Tags:

java

jaxb

jaxb2

I have a bunch of JAXB annotated classes that have a field in common, so I moved that field to a super class, like this

public class Base {
    protected SomeType commonField;
}

@XmlRootElement(name = "foo") @XmlType(propOrder = { "commonField", "fooField" })
public class Foo extends Base {
    private SomeOtherType fooField;
}

@XmlRootElement(name = "bar") @XmlType(propOrder = { "commonField", "barField" })
public class Bar extends Base {
    private SomeOtherType barField;
}

Now whenever I marshall one of Foo or Bar I get an IllegalAnnotationException complaining about commonField being listed in propOrder but not present in the class. Removing it from the propOrder annotation everything works fine, but I thougt I was supposed to list all of the mapped fields. What am I missing?

like image 850
agnul Avatar asked Jul 22 '11 12:07

agnul


People also ask

What is @XmlType annotation in Java?

If class is annotated with @XmlType(name="") , it is mapped to an anonymous type otherwise, the class name maps to a complex type name. The XmlName() annotation element can be used to customize the name. Properties and fields that are mapped to elements are mapped to a content model within a complex type.

Which tag represents the root element for the XML document in JAXB?

@XmlRootElement annotation can be used to map a class or enum type to XML type. When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.

What is @XmlElement in Java?

Maps a JavaBean property to a XML element derived from property name. Usage. @XmlElement annotation can be used with the following program elements: a JavaBean property. non static, non transient field.

What is @XmlRootElement?

Annotation Type XmlRootElementMaps a class or an enum type to an XML element. Usage. The @XmlRootElement annotation can be used with the following program elements: a top level class. an enum type.


1 Answers

The fields/properties from the inherited class will always appear before the fields/properties on the child classes. This means that by default you can not specify them in the propOrder on the child type. If however you mark the parent class as @XmlTransient the fields/properties will be treated as belonging to the child classes and can be included in the propOrder.

  • http://bdoughan.blogspot.com/2011/06/ignoring-inheritance-with-xmltransient.html
like image 77
bdoughan Avatar answered Sep 24 '22 08:09

bdoughan