JAXB and how to work around duplicate variables

For our current adf project we’re generating webservice proxy clients against the wsdls of our OSB services.
In one of the services we had the next xsd construction :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:complexType name="EmployeeType">
		<xs:annotation>
			<xs:documentation xml:lang="nl">XML Represenation of the Employee domain object</xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="id" type="xs:int" minOccurs="0">
				<xs:annotation>
					<xs:appinfo>
						<jxb:property name="idElement" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"/>
					</xs:appinfo>
				</xs:annotation>			
			</xs:element>
			<xs:element name="firstname" type="xs:string"/>
			<xs:element name="lastname" type="xs:string"/>
			<xs:element name="birthdate" type="xs:date"/>
		</xs:sequence>
		<xs:attribute name="id" type="xs:int"/>
	</xs:complexType>
</xs:schema>

The object contains both an id-element and an id-attribute.
When trying to generate the Webservice proxy against the wsdl containing this definition, JDeveloper will generate the next error :
Error creating model from wsdl “file:/EmployeeService.wsdl”: The following location is relevant to the above errorProperty “Id” is already defined. Use to resolve this conflict.
JAXB tries to generate the java representation of this schema and will (try to) generate a java class in which it will generate 2 variables with the name of ‘id’.

The wizard also comes with the solution on how we could implement the fix for this.
Change the schema to :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" <b>xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"</b>>
	<xs:complexType name="EmployeeType">
		<xs:annotation>
			<xs:documentation xml:lang="nl">XML Represenation of the Employee domain object</xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="id" type="xs:int" minOccurs="0">
				<xs:annotation><b>
					<xs:appinfo>
						<jaxb:property name="idElement"/>
					</xs:appinfo>
					</b>
				</xs:annotation>			
			</xs:element>
			<xs:element name="firstname" type="xs:string"/>
			<xs:element name="lastname" type="xs:string"/>
			<xs:element name="birthdate" type="xs:date"/>
		</xs:sequence>
		<xs:attribute name="id" type="xs:int"/>
	</xs:complexType>
</xs:schema>

We added a few things.

  • add xmlns:jaxb=”http://java.sun.com/xml/ns/jaxb” jaxb:version=”2.0 to the schema-tag
  • add the xs:appinfo-element (and childs) to the “xs:element name=”id” element

or make use of an external binding file

<jxb:bindings version="1.0" 
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
		<jxb:bindings node=".//xs:element[@name='id']">
			<jxb:property name="idElement"/>
		</jxb:bindings>
</jxb:bindings>

With this property we can force jaxb to generate a variable idElement instead of the id-element. So no duplicates on variable-name will get generated.
This will result in the next java code fragment :

public class EmployeeType {

    @XmlElement(name = "id")
    protected BigInteger idElement;
    protected String firstname;
	protected String lastname;
	protected XMLGregorianCalendar birthdate;
    @XmlAttribute
    protected BigInteger id;

No duplicates in variables, since it generated idElement and id.

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

2 Responses to “JAXB and how to work around duplicate variables”

commenter

It would be helpful if you could say how to use the external binding in netbeans

commenter

It’s been a while back since i wrote this blog and currently i don’t use Netbeans.
But are these blogs any help for you :
http://netbeans.org/kb/docs/websvc/jaxb.html
http://wiki.netbeans.org/NB6JAXBSample1

I guess you can add the binding file by use of the new file wizard in Netbeans. After that you could copy/paste the content of for example the blog in it.

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>