Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Mapping to a Type or Element/Default Root Element"

From Eclipsepedia

Jump to: navigation, search
m (New page: {{EclipseLink_UserGuide |info=y }} =Default Root Element= You configure a default root element so that the EclipseLink runtime knows the data source data type associated with the class the...)
 
m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level001.htm')
 
(30 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level001.htm
|info=y
+
}}
+
=Default Root Element=
+
You configure a default root element so that the EclipseLink runtime knows the data source data type associated with the class the descriptor describes.
+
{{EclipseLink_Note
+
|note=The undefined document root element of a referenced object is ignored during marshalling with an any collection mapping and object mapping.
+
}}
+
 
+
 
+
This section describes what a default root element is and how EclipseLink uses it.
+
 
+
Consider the <tt>Customer</tt> and <tt>Address</tt> classes and their mappings, shown in this example:
+
 
+
 
+
<span id="Example 14-1"></span>
+
''''' Customer and Address Classes'''''
+
'''<tt>Class:</tt>''' Customer
+
+
'''<tt>Default Root:</tt>''' customer
+
'''<tt>Attributes and Mappings:</tt>'''
+
    name:String                Direct Mapping to              name/text()
+
    billingAddress:Address    Composite Object Mapping to    billing-address
+
    shippingAddress:Address    Composite Object Mapping to    shipping-address
+
+
'''<tt>Class:</tt>''' Address
+
'''<tt>Default Root:</tt>''' address
+
'''<tt>Attributes and Mappings:</tt>'''
+
    street:String              Direct Mapping to              street/text()
+
    city:String                Direct Mapping to              city/text()
+
+
 
+
 
+
These classes correspond to the XML schema, shown in this example.
+
 
+
<span id="Example 14-2"></span>
+
''''' Customer and Address Schema'''''
+
<source lang="xml">
+
<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
    <xsd:complexType name="address-type">
+
        <xsd:sequence>
+
+
            <element name="street" type="xsd:string"/>
+
            <element name="city" type="xsd:string"/>
+
        </xsd:sequence>
+
    </xsd:complexType>
+
    <xsd:element name="customer" type="customer-type"/>
+
    <xsd:complexType name="customer-type">
+
+
        <xsd:sequence>
+
            <xsd:element name="name" type="xsd:string"/>
+
            <xsd:element name="billing-address" type="address-type"/>
+
            <xsd:element name="shipping-address" type="address-type"/>
+
        </xsd:sequence>
+
    </xsd:complexType>
+
+
</xsd:schema>
+
</source>
+
 
+
When an instance of the <tt>Customer</tt> class is persisted to XML, the EclipseLink runtime performs the following:
+
<ol>
+
<li> Gets the default root element.The <tt>Customer</tt> class instance corresponds to the root of the XML document. The EclipseLink runtime uses the default root element specified on the descriptor (<tt>customer</tt>) to start the XML document. EclipseLink then uses the mappings on the descriptor to marshal the object's attributes:
+
<div class="pre">
+
<customer>
+
    <name>…</name>
+
</customer>
+
</div>
+
</li>
+
<li> When the EclipseLink runtime encounters an object attribute such as <tt>billingAddress</tt>, it checks the mapping associated with it to determine with what element (<tt>billing-address</tt>) to continue:
+
<div class="pre">
+
<customer>
+
    <name>…</name>
+
    <billing-address/>
+
</customer>
+
</div>
+
<br>
+
The EclipseLink runtime checks the mapping's reference descriptor (<tt>Address</tt>) to determine what attributes to persist:
+
<div class="pre">
+
<customer>
+
    <name>…</name>
+
    <billing-address>
+
        <street>…</street>
+
        <city>…</city>
+
+
    </billing-address>
+
</customer>
+
</div>
+
</li>
+
</ol>
+
 
+
{{EclipseLink_MOXy
+
}}
+

Latest revision as of 09:49, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level001.htm