JAXB xsd:include and separate packages

♀尐吖头ヾ 提交于 2020-01-01 10:46:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4274482/jaxb-xsdinclude-and-separate-packages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!