Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/DesignDocs/317962/Phase2.1"

(Class Extractor)
(org.example.MyClassExtractor.java)
Line 226: Line 226:
 
     }
 
     }
 
}
 
}
 +
</source>
  
 
=== xml-class-extraction-method ===
 
=== xml-class-extraction-method ===
Line 240: Line 241:
 
</java-type>
 
</java-type>
 
</source>
 
</source>
 +
 
====== org.example.Employee.java ======
 
====== org.example.Employee.java ======
 
<source lang="java">
 
<source lang="java">

Revision as of 13:32, 28 July 2010

Phase 2.1 - Additional MOXy external metadata support

This phase of development involves providing additional MOXy external metadata support that will allow configuration similar to that of deployment XML

XML Metadata Tags

The following XML metadata tags will be targeted in this phase:

XML Metadata Tag MOXy Annotation Package Type Field Method
xml-property XmlProperty X X X
xml-transformation XmlTransformation X X
xml-read-transformer XmlReadTransformer X X
xml-write-transformer XmlWriteTransformer X X
xml-class-extractor XmlClassExtractor X

xml-property

Purpose

The xml-property element provides a means to set a property on a mapping or descriptor.

XML Schema

Following are the proposed schema structures for xml-property:

<xs:element name="xml-property" type="xml-property" />
<xs:complexType name="xml-property">
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="value" type="xs:string" use="required" />
    <xs:attribute name="value-type" type="xs:string" />
</xs:complexType>

An element ref to the new global xml-property element will be added to:

  • java-type
  • xml-any-attribute
  • xml-attribute
  • xml-any-element
  • xml-element
  • xml-elements
  • xml-element-ref
  • xml-element-refs
  • xml-inverse-reference
  • xml-value
  • xml-transformation

Example: type-level xml-property

The following example will demonstrate how a type-level xml-property can be applied.

Setting xml-property on a type via EclipseLink XML metadata can be accomplished as follows:

<java-type name="org.example.Employee">
    <xml-property name="identifier" value="101" value-type="Integer.class" />
    <xml-property name="isTrue" value="false" value-type="Boolean.class" />
</java-type>

Example: property-level xml-property

The following example will demonstrate how a property-level xml-property can be applied.

Setting xml-property on a property via EclipseLink XML metadata can be accomplished as follows:

<java-type name="org.example.Employee">
    <java-attributes>
        <xml-element java-attribute="myelement">
            <xml-property name="isAttribute" value="false" value-type="Boolean.class" />
            <xml-property name="comment" value="this is an element" value-type="String.class" />
        </xml-element>
    </java-attributes>
</java-type>

xml-transformation

Purpose

The xml-transformation element is used to configure an org.eclipse.persistence.oxm.mappings.XMLTransformationMapping. Transformation mappings are used to create a custom mapping where one or more XML nodes can be used to create the object to be stored in a Java class's attribute.

XML Schema

Following is the proposed schema structure for xml-transformation:

<xs:element name="xml-transformation" substitutionGroup="java-attribute">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="java-attribute">
                <xs:all>
                    <xs:element name="xml-read-transformer">
                        <xs:complexType>
                            <xs:attribute name="transformer-class" type="xs:string" />
                            <xs:attribute name="method" type="xs:string" />
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="xml-write-transformer" minOccurs="0" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="xml-path" type="xs:string" />
                            <xs:attribute name="transformer-class" type="xs:string" />
                            <xs:attribute name="method" type="xs:string" />
                        </xs:complexType>
                    </xs:element>
                    <!-- xs:element name="xml-accessor-type" type="xml-access-type" minOccurs="0"/ -->
                    <xs:element name="xml-property" type="xml-property" minOccurs="0" maxOccurs="unbounded"/>
                    <xs:element name="xml-access-methods" type="xml-access-methods" minOccurs="0"/>
                </xs:all>
                <xs:attribute name="name" type="xs:string" use="required"/>
                <!-- xs:attribute name="fetch" type="orm:fetch-type"/ -->
                <xs:attribute name="optional" type="xs:boolean" default="false"/>
                <xs:attribute name="xml-accessor-type" type="xml-access-type" minOccurs="0" default="PUBLIC_MEMBER"/>
                <xs:attribute name="mutable" type="xs:boolean" default="false"/>
                <xs:attribute name="attribute-type" type="xs:string" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

Example

The following example will demonstrate how xml-transformation can be applied.

Setting xml-transformation via EclipseLink XML metadata can be accomplished as follows:

<java-type name="org.example.Employee">
    <java-attributes>
        <xml-transformation java-attribute="hours" optional="true">
            <xml-read-transformer transformer-class="org.example.NormalHoursTransformer" />
            <xml-write-transformer xml-path="normal-hours/start-time/text()" transformer-class="org.example.StartTimeTransformer"/>
            <xml-write-transformer xml-path="normal-hours/end-time/text()" transformer-class="org.example.EndTimeTransformer"/>
            <xml-access-methods get-method="getNormalHours" set-method="setNormalHours" />
        </xml-transformation>
    </java-attributes>
</java-type>

Class Extractor

A class extractor provides support for complex inheritance; it allows for a user defined class indicator in place of providing an explicit class indicator field. There are two ways to set a class extractor:

  • By providing a subclass of org.eclipse.persistence.descriptors.ClassExtractor
    • This can be set using xml-class-extractor
  • By providing a class extraction method on the domain class
    • This can be set using xml-class-extraction-method

XML Schema

Below are the proposed schema structures for xml-class-extractor and xml-class-extraction-method. A choice will be added to java-type:

<xs:choice>
    <xs:element name="xml-class-extractor">
        <xs:complexType>
            <xs:attribute name="class" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="xml-class-extraction-method">
        <xs:complexType>
            <xs:attribute name="name" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:choice>

xml-class-extractor

Purpose

A class extractor is registered with the descriptor to override the default inheritance mechanism. The instance registered must extend the org.eclipse.persistence.descriptors.ClassExtractor class and implement the extractClassFromRow(Record, Session) method; this method is used to determine which class to instantiate when unmarshalling.

Example

The following example will demonstrate how the xml-class-extractor can be applied.

Setting xml-class-extractor via EclipseLink XML metadata can be accomplished as follows:

<java-type name="org.example.Person">
    <xml-class-extractor class="org.example.MyClassExtractor" />
</java-type>
org.example.Person.java
package org.example;
 
class Person {
    String name;
}
org.example.Employee.java
package org.example;
 
class Employee extends Person {
    String id;
}
org.example.MyClassExtractor.java
package org.example;
 
class MyClassExtractor extends org.eclipse.persistence.descriptors.ClassExtractor {
    public static Class extractClassFromRow(Record databaseRow, Session session) {
        return Employee.class;
    }
}

xml-class-extraction-method

Purpose

The class extraction method is registered with the descriptor to override the default inheritance mechanism. The method, which has a Record as its only input parameter and is assumed to exist on the descriptor, is used to determine which class to instantiate when unmarshalling.

Example

The following example will demonstrate how the xml-class-extraction-method can be applied.

Setting xml-class-extraction-method via EclipseLink XML metadata can be accomplished as follows:

<java-type name="org.example.Person">
    <xml-class-extraction-method name="getClassForRow" />
</java-type>
org.example.Employee.java
package org.example;
 
class Employee extends Person {
    String id;
}
org.example.Person.java
package org.example;
 
class Person {
    String name;
 
    public static Class getClassForRow(Record databaseRow) {
        return Employee.class;
    }
}

Back to the top