Understanding EclipseLink, 2.5
  Go To Table Of Contents
 Search
 PDF

Object-XML Mapping Concepts

This section describes concepts for nonrelational mappings that are unique to EclipseLink:

Simple Type Translation

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.


NoteNote:

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.

Figure 7-12 Simple Type Translation

Description of Figure 7-12 follows
Description of "Figure 7-12 Simple Type Translation"

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.

Default Read Conversions

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


Default Write Conversions

Table 7-3 lists the built-in conversion pairs for writing XML. When a Java class attribute is of a type in Table 7-3 and the simple type translation is enabled, the corresponding schema type is specified on the element written.

Table 7-3 Simple Type Translation Write Conversions

Java Type Schema Type

Byte[]

hexBinary

BigInteger

integer

Boolean

boolean

Byte

byte

Calendar

dateTime

GregorianCalendar

dateTime

Double

double

Float

float

Integer

int

Long

long

int

int

short

short

String

string