This discussion is archived
0 Replies Latest reply: Feb 21, 2012 5:04 PM by 919061 RSS

Marshalling Objects extending JAXBElement linked by @XmlElementRef

919061 Newbie
Currently Being Moderated
As described here, there is a difference, how JAXB will generate classes: link

We have the following schema files:

Schema "references.xsd" with reusable elements:
-----
<element name="foo" type="common:sometype"/>
<element name="bar" type="common:sometype"/>
-----
Schema "object.xsd" with some real constructs like:
-----
<element name="object">
<complexttype>
<choice>
<element ref="references:foo"/>
<element ref="references:bar"/>
</choice>
</complextype>
</element>
-----
JAXB will generate a class for the element object and may name it "Object" with an @XmlRootElement annotation on it, while the property for "foo" and "bar" will get optimized the the type "common:type". So we will loose the information about the element "foo" and "bar", which causes problems, because this is a choice and JAXB will no longer be able to differ between them. The List accepts the generated class for "common:sometype" twice. Because of this, I added the following bindings to the binding.xjb:
-----
<bindings schemaLocation="references.xsd">
<bindings node="//xs:element[@name='foo']">
<class name="Foo"/>
</bindings>
<bindings node="//xs:element[@name='bar']">
<class name="Bar"/>
</bindings>
</bindings>
-----
Now, JAXB will generate these two classes "Foo" and "Bar" instead of directly using the "common:sometype"-class for these properties and the choice can be marshalled. This issue is not mainly about the "xs:choice", btw.

The problem is, that the JAXBContext cannot find these two classes in the contextPath, because they look like this:
-----
public class Foo extends JAXBElement<CommonSomeType> {
private QName qname = ....
}
-----
When analyzing JAXBContext.toString() I also do not see them in the list of the known classes.

In this example, the generated Object class looks like this:
-----
@XmlRootElement(...)
public class Object {
@XmlElementRef(name="foo", namespace="...", type=Foo.class)
private Foo foo;
}
-----
How am I supposed to be able to marshal classes, that have properties annotated with @XmlElementRef, which link to classes like Foo?

Edit: Btw, marshalling classes like Foo works if you directly marshal them, if the class CommonSomeType is know to the JAXBContext. It just does not work, if classes like Foo are linked by @XmlElementRef. I guess this is a bug in the JAXB implementation.

Thank you in advance.

Edited by: 916058 on 21.02.2012 16:42

Edited by: 916058 on 21.02.2012 17:02

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points