users@jaxb.java.net

Re: JAXB 2.0 union

From: Joe Fialli <Joseph.Fialli_at_Sun.COM>
Date: Tue, 19 Jul 2005 13:46:17 -0400

Scott Allan wrote:

> I'm testing out the early access JAXB 2.0. I have this datatype
> defined in my schema which currently works in 1.0 but I am getting a
> different result in 2.0. I would expect that if a have an element
> assigned to this "dateType" that I could put in either java sql.Date
> value or a java.lang.String value such as:
>
Handling for union has changed between JAXB 1.0 and JAXB 2.0.

 From ChangeLog in JAXB 2.0 Early Draft 2:

    XSD to Java Binding

        Simplified following bindings:
        - union by binding to String rather than Object.

Rationale for change is that it is quite complex computation to take an
XML data value and compute
which union membertype it matches and then convert that string to that
data type. JAXB 1.0
generated specialized code to do this for each union. A goal in JAXB
2.0 was to reduce
the large amounts of generated specialized code. While there is an
observable reduction in functionality,
it was judged that the benefits of lower complexity in generated code
and faster unmarshal/marshal times when
the data was not even accessed were worth the change.

-Joe Fialli, Sun Microsystems

> myObject.setMyField(mySqlDate);
>
> I'm getting this error
> Error(196): setMyField(java.lang.String) in myPackage.TestType cannot
> be applied to (java.sql.Date)
>
> <xs:simpleType name="dateType">
> <xs:union>
> <xs:simpleType>
> <xs:annotation>
> <xs:appinfo>
> <jaxb:javaType name="java.sql.Date"
> parseMethod="valueOf"
> printMethod="toString"/>
> <!-- sample format 2003-10-13 -->
> </xs:appinfo>
> </xs:annotation>
> <xs:restriction base="xs:date"/>
> </xs:simpleType>
> <xs:simpleType>
> <xs:restriction base="xs:string">
> </xs:restriction>
> </xs:simpleType>
> </xs:union>
> </xs:simpleType>
>