I\'m upgrading a project to jaxb 2.2.7 from version 1.x.
I\'ve got the app working some of the time, but on some responses I see this:
java.lang.Runt
I got the same exception when attempting to marshal some jaxb obejcts that I had generated from the hibernate-mapping-4.0.xsd
The exceptions that were being thrown seemed to involve two classes that were generated as inner classes of the class HibernateMapping root class - "Id" and "CompositeID". Both of these elements were defined in the XSD as nested complexTypes, just as in @mdarwin's case. By moving the complexType definitions out (so that they were root elements of the "schema" element in the xsd) then the problem was resolved and the objects were successfully marshaled.
A shame as I would have liked to have used the unmodified XSD to generate my jaxb object - but could not find another way around the problem.
The case that I noted does not depend on <xs:complexType>
definitions in xsd-schema.
The actual problem was that we have Java classes that extend from a class generated from xml-schema.
And these classes are annotated with @XmlType(name = "")
to make them anonymous (i.e. that generated tag does not contain xsi:type
attribute and the generated xml-file remains valid for the initial-schema.
I found the clue to this solution in java, xsd & marshalling: jre bug, my fault or xsd issues?.
Since I can't modify the xsd (it is too complex and is already shared to the clients of our APIs), the solution is:
Make a jaxb-binder that excludes the complexType to be generated into Java file and says JAXB to use the existing class instead. Binding file looks like this:
<jxb:bindings schemaLocation="MySchema.xsd">
<jxb:bindings node="//xs:complexType[@name='TheClassYouWantToExclude']">
<jxb:class ref="my.existing.class.TheClassYouWantToExclude"/>
</jxb:bindings>
</jxb:bindings>
In existing class, the class that we need to extend is NOT a JAXB-annotated class, just an abstract class:
package my.existing.class;
public abstract class TheClassFromWhichYouWantToExtend {
}
@XmlAnyElement(lax = true)
:
package my.existing.class;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(...)
public class TheClassYouWantToExclude {
// ...
@XmlAnyElement(lax = true)
protected TheClassFromWhichYouWantToExtend theClassFromWhichYouWantToExtend;
}
The problem turned out to be the elaborate nesting of anonymous complex types.
By separating them out as below, the problem went away. And as an added bonus, I got more re-usable code.
<xs:complexType name="balanceImpactRate">
<xs:sequence>
<xs:element name="rate" type="xs:double" />
</xs:sequence>
<xs:attribute name="charging-resource-code" type="xs:string"
use="required" />
</xs:complexType>
<xs:complexType name="balanceImpactRates" >
<xs:sequence>
<xs:element name="balance-impact-rate" type="balanceImpactRate" minOccurs="0"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>