6

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Alexander
  • 553
  • 5
  • 9

1 Answers1

6

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks for the reply. I've already changed it to use import instead of include, indeed seems to be better. I will take a look at the blog post, seems promising so far. – Alexander Nov 25 '10 at 13:49
  • This solved my problem. Thanks for the link, I've been search for days now. :S – Alexander Nov 25 '10 at 14:13
  • 1
    using import instead of include may have worked for this particular case but the exact same problem with the ObjectFactory containing duplicate methods can show up when using import as well (if object references are used). See: http://stackoverflow.com/questions/5155414/jaxb-multiple-schemas-with-element-reference – Marcus Junius Brutus May 30 '13 at 11:41