29

I have two schemas which are processed using JAXB. The first schema is preprocessed and information of this is used using an episode file (following http://www.java.net/blog/2006/09/05/separate-compilation-jaxb-ri-21). The second schema imports the first, and again using jaxb, is processed. This all works as expected.

But now I have an element in the first schema, which is used in the second using a reference.

Schema a:

<schema elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Test/">
<element name="type" type="test:MyType"></element>

Schema b:

<schema elementFormDefault="qualified" 
xmlns="http://www.w3.org/2001/XMLSchema" 
xmlns:second="http://www.example.org/Second/"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Second/">

<import namespace="http://www.example.org/Test/" />

<complexType name="SomeType">
    <sequence>
        <element ref="test:type" minOccurs="1" maxOccurs="unbounded" />
    </sequence>
</complexType>

During processing nothing is wrong, but the generated code for both schemas provide the same method:

public JAXBElement<EventType> createType(TypeType value)

At runtime, this results in the following error:

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

How can I prevent JAXB from creating the duplicate createType methods?

Thanks in advance!

Update: I asked this same question on the JAXB mailing list, on that list I also posted a working example. The thread and example can be found at: http://java.net/projects/jaxb/lists/users/archive/2011-03/message/18

On this list I've been suggested a workaround, and now I can use the schemas the way I like. But I still think JAXB should not create the additional "create" method, since it should already be in the episode file.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
Alexander
  • 553
  • 5
  • 9
  • 1
    What are your arguments to xjc? – justkt Mar 01 '11 at 14:03
  • For the second schema I use XJC from ant, and the episode file is in a jar, which is supplied as an argument. And additionally there is a catalog to be able to map to the first schema. This seems to work, if I do not use the jar with episode file, the second schema is used to generate all types. While in my example, only the createType(TypeType..) method is duplicated. – Alexander Mar 01 '11 at 14:29
  • 1
    Please present a complete schema. What definition is "test:MyType" ? – Yu Sun corn Mar 04 '11 at 06:45
  • I've updated my question with a link (and a usable workaround to the question). Even though I can now work with the generated code, I still don't understand why jaxb would create the additional ObjectFactory. – Alexander Mar 14 '11 at 06:37
  • 3
    Software has bugs. I'd submit the case to JAXB bug tracker. Chances are it will be fixed (or documented as a feature) in the next release. – Vladimir Dyuzhev Mar 25 '11 at 14:50
  • Can you process each schema individually to see if they achieve different types - instead of importing the secondary schema into the primary schema. Been able to achieve difference type independently will indicate JAXB having issue processing imports. – Bitmap Apr 21 '11 at 08:47
  • @Alexander: I visited the link. Indeed, JAXBContext.newInstance("example.a:example.b") results in a runtime exception whereas JAXBContext.newInstance(example.b.ObjectFactory.class) works. Was that the approach that you used? Cause in that case, it's not clear what to do if both Object Factories are needed. – Marcus Junius Brutus May 30 '13 at 13:37
  • Posted a bug report on that problem on JAXB RI bug tracker: https://java.net/jira/browse/JAXB-962 – Marcus Junius Brutus Jun 15 '13 at 12:40

1 Answers1

1

I've written a few Schema Definitions in my day. You are declaring your first xsd in your second schema declaration and then you are importing it.

As per MSDN, when you import an XSD you do not include it in the Schema Declaration. This is where it is in your schema declaration.

xmlns:test="http://www.example.org/Test/"

Remove this and just do the import... ( <xs:import namespace="http://www.example.com/IPO" /> )

see:http://msdn.microsoft.com/en-us/library/ms256480.aspx

MByD
  • 135,866
  • 28
  • 264
  • 277
Andrew Carr
  • 777
  • 6
  • 16
  • I tried that in Linux/Java-land and both the xmllint command line utility and the JAXB xjc compiler Ant task complained with the same message more or less. Both the import and the prefix declaration appear to be necessary. It's a bit spooky that MSDN behaves differently. – Marcus Junius Brutus May 30 '13 at 11:22