This section describes concepts for nonrelational mappings that are unique to EclipseLink:
The simple type translation can be used with direct and direct collection mappings, allowing you to automatically translate an XML element value to an appropriate Java type based on the element's <type>
attribute as defined in your XML schema.
Note: Simple type translation is applicable only for XML projects. |
In code, this is set up when building a mapping using the setIsTypedTextField
method on XMLField
, for example:
XMLDirectMapping mapping = new XMLDirectMapping(); XMLField tef = new XMLField(); tef.setIsTypedTextField(true); tef.setXPath("NUMBER/text()"); mapping.setField(tef); mapping.setAttributeName("number"); xmlDescriptor.addMapping(mapping);
You can use simple type translation only when the mapping's XPath goes to a text node. You cannot use a simple type translation if the mapping's XPath goes to an attribute.
Using simple type translation, you can make the XML document preserve type information. This is useful when your object model specifies generic object attributes such as java.lang.Object
and java.io.Serializable
, since they do not trigger specific type conversions in EclipseLink as do specific object attributes such as java.lang.Integer
or java.util.Calendar
.
Figure 7-12 illustrates a type translation XML mapping for the number
attribute of the PhoneNumber
class. Notice that the Java attribute is not specific enough to preserve the typing. Simple type translation adds the type information to the resulting document to preserve the typing.
By default, EclipseLink uses built-in read and write conversion pairs (see Default Read Conversions and Default Write Conversions).
You can override this behavior by specifying and configuring your own simple type translation, for example, to write XML binary data as Base64
.
Table 7-2 lists the built-in conversion pairs for reading XML elements. When the schema <type>
attribute is specified and simple type translation is enabled, the value read is converted to the corresponding Java type.
Table 7-2 Simple Type Translation Read Conversions
Schema Type | Java Type |
---|---|
base64Binary |
Byte[] |
boolean |
Boolean |
byte |
Byte |
date |
Calendar |
dateTime |
Calendar |
double |
Double |
float |
Float |
hexBinary |
Byte[] |
int |
int |
integer |
BigInteger |
long |
Long |
short |
Short |
string |
String |
time |
Calendar |
unsignedByte |
Short |
unsignedInt |
Long |
unsignedShort |
Integer |