Skip to content
Advertisement

xjc: Two declarations cause a collision in the ObjectFactory class

Running the following xjc command raises an error :

JavaScript

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:

JavaScript

Advertisement

Answer

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

JavaScript

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

JavaScript

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).

Advertisement