Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB xsd:include and separate packages

Tags:

java

jaxb

xsd

I have the following situation:

There are 2 xsd files. The 1st one defines a root element, and several types.

The second one includes the first, and extends one of the types. There is no root type defined in this file.

From the first xsd, a model is generated in a package (a). The second schema should create a new package (b) for the additional types, but reuse the generated package a. I solved this by using a binding file which points to the previously generated elements (in package a). So far this works, but..

JAXB generates a ObjectFactory in package A, which contains a create method for the root element. For the second schema, also an ObjectFactory is created in package B. And this class also had the create method for the same root element.

To be able to use all types, the jaxb context is created using multiple object factories (newInstance(a.ObjectFactory.class, b.ObjectFactory.class)).

At runtime this results in the following error:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions The element name {http://www.example.org/Scenario/}scenario has more than one mapping

Should I generate the packages differently? Or is there something possible using the binding file to prevent the object factory from having duplicate methods?

like image 343
Alexander Avatar asked Nov 25 '10 07:11

Alexander


1 Answers

First of all, it is important to understand that if you're using xsd:include instead of xsd:import, you don't have two different schemas. It's one schema in several files and compiling it in several packages and tricking JAXB to combine these packages looks more like hacking.

So my primary suggestion would be to use xsd:import instead and consider separate schema compilation approach.

If you want to stay with xsd:include, you'll have to trick JAXB. For instance, you can remove or tweak one (or both) of the ObjectFactory classes and build you JAXB context based on individual classes rather than object factories. You can also use jaxb.index instead of object factories. But it's all hacking.

like image 53
lexicore Avatar answered Oct 02 '22 23:10

lexicore