30

Running the following xjc command raises an error :

$ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

Although I understand the JAXB bindings and what are is conflict in XJC, I don't understand where is the conflict in the current schema.

how should I fix this ?

Thanks,

Pierre

update: here is the context of the errors:

$ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)' 
   330  <xs:element maxOccurs="1" name="Description"
   331  type="xs:string" minOccurs="0">
   332  <xs:annotation>
   333  <xs:documentation>
   334  Optionally provide description especially when "eOther" is selected
   335  </xs:documentation>
   336  </xs:annotation>
   337  </xs:element>
   338  <xs:element name="BioSampleSet" minOccurs="0" maxOccurs="1"><xs:annotation><xs:documentation>Identifier of the BioSample when known</xs:documentation>
   339  </xs:annotation>
   340  <xs:complexType><xs:sequence><xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
   341  </xs:sequence>
   342  </xs:complexType>
   343  </xs:element>
   344  </xs:sequence>
   345  <xs:attribute name="sample_scope" use="required">
   346  <xs:annotation>
   347  <xs:documentation>
   348  The scope and purity of the biological sample used for the study
   349  </xs:documentation>
   350  </xs:annotation>
--
   465  <xs:documentation>Please,  fill Description element when choose "eOther"</xs:documentation>
   466  </xs:annotation>
   467  </xs:enumeration>
   468  </xs:restriction>
   469  </xs:simpleType>
   470  </xs:attribute>
   471  </xs:complexType>
   472  </xs:element>
   473  <xs:element name="TargetBioSampleSet">
   474  <xs:annotation><xs:documentation>Set of Targets references to BioSamples</xs:documentation></xs:annotation>
   475  <xs:complexType>
   476  <xs:sequence>
   477  <xs:element name="ID" type="xs:token" minOccurs="1" maxOccurs="unbounded"></xs:element>                                                 
   478  </xs:sequence>
   479  </xs:complexType>
   480  </xs:element>                        
   481  </xs:choice>
   482  <xs:element name="Method" minOccurs="1">
   483  <xs:annotation>
   484  <xs:documentation>
   485  The core experimental approach used to obtain the data that is submitted to archival databases
2
  • i don't think anyone can help you if you do not provide relevant parts from Core.xsd
    – hoaz
    Commented Nov 16, 2012 at 18:51
  • Also try using the newer Java JDK to generate them. I had a lot of duplicate errors using JDK 1.7 until I used xjc in JDK 1.8. Also in case someone is trying to generate by right click > Generate in Eclipse, make sure to try it from the command line too. Doing this resolved errors for me.
    – Kt Mack
    Commented Sep 5, 2018 at 20:24

6 Answers 6

34

I'll quote from the most official unofficial guide on JAXB on the net.

When schemas contain similar looking element/type names, they can result in "Two declarations cause a collision in the ObjectFactory class" errors. To be more precise, for each of all types and many elements (exactly what elements get a factory and what doesn't is bit tricky to explain), XJC produces one method on the ObjectFactory class in the same package. The ObjectFactory class is created for each package that XJC generates some files into. The name of the method is derived from XML element/type names, and the error is reported if two elements/types try to generate the same method name.

That said, you have two options.

The first is to define an external binding XML like this

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="1.0">
  <jaxb:bindings schemaLocation="Core.xsd">
    <jaxb:bindings node="//xs:element[@name='BioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeBioSampleSet"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xs:element[@name='TargetBioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeTargetBioSampleSet"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

In the generated ObjectFactory class this will create two methods called createTypeBioSampleSet and createTypeTargetBioSampleSet (JAXB will append the name you specify to the word create) that can be used to produce BioSampleSet and TargetBioSampleSet objects.

(It's not necessary to define a binding for both types.)

I'm not exactly sure why JAXB refuses to generate classes from the given schema, but when I specified only one binding (for BioSampleSet for example) then the other type's factory method was named like createTypeProjectProjectTypeSubmissionWhateverThisAndThatTargetTargetSampleBioCatDogWoofTypeIDoNotKnowWhatElse so I think JAXB choked on this long method identifier, because it somehow managed to create the same one for both types. I think this is some implementation detail in JAXB.

The other solution is to create a base type for a BioSampleSet and use that at both locations like this

<xs:element name="ProjectTypeSubmission">

...

  <xs:element name="Target">

    ...

    <xs:element name="BioSampleSet" type="typeBioSampleSet" minOccurs="0" maxOccurs="1"/>

    ...

  </xs:element>

  ...

  <xs:element name="TargetBioSampleSet" type="typeBioSampleSet"/>

  ...

<xs:element/>

...

<xs:complexType name="typeBioSampleSet">
  <xs:sequence>
    <xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
  </xs:sequence>
</xs:complexType>

The best solution would be to drop every anonymous type declarations from your schema. If you can do that, do it, because this schema looks like a mess (to me at least).

2
9

remove -p package in the command

xjc -d src -XautoNameResolution TFMData_Service.xsd
2
  • It's the most easy solution. Couldn't figure out why I still got collisions although -XautoNameResolution was used. Removing the package option is the key.
    – ka3ak
    Commented Apr 20, 2021 at 10:00
  • 1
    for those who are banging head against how to "remove -P", my original xjc command [in build.gradle] was like "xjc(destdir: sourcesDir, schema: schema, package: "com.example.consumingwebservice.wsdl" )". removing package means my xjc command becomes "xjc(destdir: sourcesDir, schema: schema )".
    – Jach
    Commented Mar 10, 2023 at 9:38
3

The solution that worked for me when using the below maven plugin is adding -XautoNameResolution

<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>

Sample code:

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.5.0</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <xjbSources>
                    <xjbSource>src/main/resources/global.xjb</xjbSource>
                </xjbSources>
                <sources>
                    <source>src/main/xsd/</source>
                </sources>
                <arguments>
                    <argument>-XautoNameResolution</argument>
                </arguments>
                <packageName>com.xxx.yyy.zzz.policy.xml</packageName>
            </configuration>
        </plugin>
1

I had the same problem, collision of object factory, and my solution was to use the following binding:

<jaxb:bindings version="1.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               jaxb:extensionBindingPrefixes="xjc">

    <jaxb:bindings
            schemaLocation="../pfel_xsd/pfel-mng-managedocument-v4-vs_data.xsd"
            node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="document.client.pfelservice"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings
            schemaLocation="../pfel_xsd/pfel-mng-managedocument-v4-vs_data-types.xsd"
            node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="document.client.pfel"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

</jaxb:bindings>

Here's the solution: https://www.javaer101.com/en/article/1278220.html

0

I faced a similar issue with name collisions in the ObjectFactory class.

One of the problems was that in default settings XJC generates a more verbose Java model with intermediate types. This causes collisions among the required model objects and the intermediate model objects.

The solution was to use the XLC simple binding mode. This eliminates the intermediate model objects, which in turn solves the collision problem.

To enable this, add this binding file:

<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

    <jxb:globalBindings>
        <xjc:simple/>
    </jxb:globalBindings>

</jxb:bindings>

An example of how the ObjectFactory looks much simplified for the same XSD before and after the simple binding setting. (Note the Citation and CitationX elements which were colliding earlier and I had fiddled with renaming one of them to CitationX.)

enter image description here

For more information on Simple bindings, refer:

  1. https://stackoverflow.com/a/5155704/1364747
  2. https://stackoverflow.com/a/13359717/1364747
1
  • Unfortunately, this doesn't produce the proper schema model. Lots of missing classes.
    – Teddy
    Commented May 27, 2022 at 13:01
0

In my case, the cause was that I had specified a target package when running wsimport which meant that all the generated classes were in the same package. The default behaviour of wsimport is to generate code in packages based on the namespace specified in schema tags, which is what you generally want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.