+

Search Tips   |   Advanced Search

Map persistent properties to XML columns


Overview

If the database supports XML column types, we can map XML columns to a Java string or a Java byte array field.

This type of mapping is no longer a WAS for JPA extension. Instead, see Apache OpenJPA documentation. JPA for the application server supports the management of XML objects by using a third-party solution for mapping management. These mapping techniques make using the XML objects as strings or byte arrays difficult. DB2, Oracle, and SQLServer databases support XML column types, XPath queries, and indices over these columns.


Persistent properties to XML mapping

Embedded classes with XML column support use...

Path expressions and predicates over the embedded class are converted to XML predicates, XPATH expressions, or XQuery expressions, and are written to the database.

WAS v8.5.5 supports JPA applications using a third-party tool for XML mapping. This is accomplished using the extension points for custom field mappings. The third-party mapping tool uses the extension points, providing a custom value handler for the persistent fields mapped to the XML columns. In OpenJPA, this value handler...

This handler requires the @Strategy annotation on the Java field that is mapped to the XML column.

  1. Annotate the entity property using the XML value handler strategy. The mapping of persistent properties to XML columns requires the @Strategy and the @Persistent annotation.
    @Persistent
    @Strategy("org.apache.openjpa.xmlmapping.XmlValueHandler")
    

    The XML value handler for the persistent property is set to...

      org.apache.openjpa.xmlmapping.XmlValueHandler

  2. Change the default for fetch type if it is necessary. For example:

      @Persistence(fetch=FetchType.LAZY)

    The fetch type is now LAZY. If a value for the fetch type is not entered, the default is set to EAGER.

  3. Annotate the embedded classes with the binding annotations for JAXB. These bindings can be created from an XML schema using the Java Architecture for XML Binding Compiler (XJC).

  4. Make sure the class that maps to the root of the XML document is annotated with @XmlRootElement, in addition to the other annotations.

  5. Compile the Java sources.

  6. Run the enhancer tool on the entities. Refer to the topic on the entity enhancer tool for more information.


Example

For example, shipAddress, a property of Order Entity, is mapped to XML column shipaddr:

@Entity
public class Order {
 @Id   @GeneratedValue(strategy=GenerationType.IDENTITY)
  int oid;
 @Persistent
 @Strategy("org.apache.openjpa.xmlmapping.XmlValueHandler")
      @Column(name="shipaddr")
      Address shipAddress;

The OpenJPA mapping tool generates a SHIPADDR column with XML type in the table definition for ORDER table.


Related tasks

  • Develop JPA 2.x applications for a Java SE environment
  • Develop JPA 2.x applications for a Java EE environment

  • wsenhancer command


    Related information:

    Apache OpenJPA User Guide: XML Mapping