Skip Headers

# Mapping an xsd:union Type

Previous

Next

Use an `EISDirectMapping` (with XML records) or an `XMLDirectMapping` to map a Java attribute to an `xsd:union` type, such as:

        <xsd:simpleType name="size-type">
            <xsd:union memberTypes="xsd:decimal xsd:string"/>
        </xsd:simpleType>

When TopLink marshalls (writes) an object to XML, it uses its default conversion pairs to convert from the Java type to the appropriate `xsd` type.

In the case where the `memberTypes` map to the same Java type, TopLink marshalls using the first `memberType` in the union which allows a successful conversion. For example, if you map a Java type of `byte[]` to an `xsd:union` with `memberTypes` of `hexBinary` and `base64Binary`, then TopLink marshalls using the first `memberType`: `hexBinary`.

You can customize the default conversion pairs to control the Java type to `xsd` type conversion using `XMLField` method `addConversion` and configuring your mapping with that `XMLField` using `EISDirectMapping` or `XMLDirectMapping` method `setField`. For example, if the `memberTypes` were `xsd:date` and `xsd:time` and the Java attribute was of type `java.util.Date` instead of the JAXB 1.0 standard `java.util.Calendar`, you can modify the conversion pair for `xsd:date` to be `java.util.Date`.

When TopLink unmarshalls (reads) XML into an object, it tries each `memberType` in the order specified in the XSD until the first successful conversion is made.

If your XML document specifies the `xsi:type` attribute on an element, then TopLink converts according to the `xsi:type` instead of trying the `memberTypes`.

For more information, see "Mapping to a Union Field With an XML Direct Mapping". The same applies to an `EISDirectMapping` with XML records (see "EIS Direct Mapping").