Skip navigation links

Oracle Fusion Middleware Java API Reference for Oracle TopLink
11g Release 1 (11.1.1)

E28847-01


org.eclipse.persistence.sdo.helper.jaxb
Class JAXBHelperContext

java.lang.Object
  extended by org.eclipse.persistence.sdo.helper.SDOHelperContext
      extended by org.eclipse.persistence.sdo.helper.jaxb.JAXBHelperContext


public class JAXBHelperContext
extends SDOHelperContext

The JAXBHelperContext is a bridge between POJOs and SDO DataObjects. The bridge is based on their corresponding XML representations. For the POJOs the XML representation is specified using JAXB annotations or object-to-XML mappings.

The following steps are required to create the JAXBHelperContext. The XML schema used in step #3 is the same one that the POJOs are mapped to. This step has been separated so that SDO annotations could be added to the XML schema.

Step #1 - Create the JAXBContext

 JAXBContext jaxbContext = JAXBContext.newInstance("com.example.customer");
 

Step #2 - Create the JAXBHelperContext

 JAXBHelperContext jaxbHelperContext = new JAXBHelperContext(jaxbContext);
 

Step #3 - Create the SDO Metadata from an XML Schema

 jaxbHelperContext.getXSDHelper().define(xmlSchema);
 

The JAXBHelperContext allows you to convert between POJOs and DataObjects using a wrap operation.

 Customer customer = new Customer();
 Address address new Address();
 address.setStreet("123 Any Street");
 customer.set(address);
 
 DataObject customerDO = jaxbHelperContext.wrap(customer);
 customerDO.getString("address/street");  // returns "123 Any Street"
 

The JAXBHelperContext allows you to convert between DataObjects and POJOs using an unwrap operation.

 Type customerType = jaxbHelperContext.getType(Customer.class);
 DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);
 customerDO.set("first-name", "Jane");
 
 Customer customer = jaxbHelperContext.unwrap(customerDO);
 customer.getFirstName();  // returns "Jane"
 

Of course the POJOs may be JPA entities. Below is an example of wrapping the results of a JPA query.

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerExample");      
 EntityManager em = emf.createEntityManager();
 List<MyEntity> entities = em.createQuery("SELECT e FROM MyEntity e WHERE ...").getResultList();
 List<DataObject> dataObjects = hc.wrap(entities);
 

Nested Class Summary

 

Nested classes/interfaces inherited from class org.eclipse.persistence.sdo.helper.SDOHelperContext
SDOHelperContext.MyNotificationFilter

 

Field Summary
private  JAXBContext jaxbContext
           
private  java.util.Map<java.lang.Object,SDODataObject> wrapperDataObjects
           

 

Fields inherited from class org.eclipse.persistence.sdo.helper.SDOHelperContext
copyHelper, dataFactory, dataHelper, equalityHelper, typeHelper, xmlHelper, xsdHelper

 

Constructor Summary
JAXBHelperContext(javax.xml.bind.JAXBContext aJAXBContext)
          Create a new instance of JAXBHelperContext
JAXBHelperContext(javax.xml.bind.JAXBContext aJAXBContext, java.lang.ClassLoader aClassLoader)
          Create a new instance of JAXBHelperContext

 

Method Summary
 java.lang.Class getClass(Type type)
          Return the wrapped class corresponding to the SDO type.
 javax.xml.bind.JAXBContext getJAXBContext()
          Return the JAXBContext.
(package private)  XMLDescriptor getObjectDescriptor(SDOType sdoType)
          Get the XML descriptor for the entity class corresponding to the SDO type.
 Type getType(java.lang.Class entityClass)
          Return the SDO type corresponding to the wrapped class.
protected  void initialize(java.lang.ClassLoader aClassLoader)
          Override the default helpers/factories, replacing them with JAXB aware ones where necessary.
(package private)  void putWrapperDataObject(java.lang.Object anObject, SDODataObject aDataObject)
          Maintain an association between this POJO and DataObject.
 java.util.List<java.lang.Object> unwrap(java.util.Collection<DataObject> dataObjects)
          Perform the unwrap operation on each of the DataObjects in the collection, and return the results as a List.
 java.lang.Object unwrap(DataObject dataObject)
          Return the POJO that is wrapped by the DataObject.
 java.util.List<DataObject> wrap(java.util.Collection<java.lang.Object> entities)
          Perform the wrap operation on each of the POJOs in the collection, and return the results as a List.
(package private)  java.util.List<DataObject> wrap(java.util.Collection<java.lang.Object> entities, Property containmentProperty, DataObject container)
          Helper method that configures container information.
 DataObject wrap(java.lang.Object entity)
          Return a DataObject that wraps a POJO.
(package private)  DataObject wrap(java.lang.Object entity, Property containmentProperty, DataObject container)
          Helper method that configures container information.

 

Methods inherited from class org.eclipse.persistence.sdo.helper.SDOHelperContext
addAlias, createResolvable, createResolvable, getCopyHelper, getDataFactory, getDataHelper, getEqualityHelper, getHelperContext, getHelperContext, getHelperContext, getIdentifier, getProperty, getTypeHelper, getXMLHelper, getXSDHelper, hasHelperContext, isApplicationResolverSet, makeDefaultContext, putHelperContext, putHelperContext, removeHelperContext, removeHelperContext, reset, setApplicationResolver, setProperty

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Field Detail

jaxbContext

private JAXBContext jaxbContext

wrapperDataObjects

private java.util.Map<java.lang.Object,SDODataObject> wrapperDataObjects

Constructor Detail

JAXBHelperContext

public JAXBHelperContext(javax.xml.bind.JAXBContext aJAXBContext)
Create a new instance of JAXBHelperContext
Parameters:
aJAXBContext - - The JAXBContext representing the class to XML schema mapping.

JAXBHelperContext

public JAXBHelperContext(javax.xml.bind.JAXBContext aJAXBContext,
                         java.lang.ClassLoader aClassLoader)
Create a new instance of JAXBHelperContext
Parameters:
aJAXBContext - - The JAXBContext representing the class to XML schema mapping.
aClassLoader - - The ClassLoader containing the generated SDO classes/interfaces (if any).

Method Detail

initialize

protected void initialize(java.lang.ClassLoader aClassLoader)
Override the default helpers/factories, replacing them with JAXB aware ones where necessary.
Overrides:
initialize in class SDOHelperContext

getJAXBContext

public javax.xml.bind.JAXBContext getJAXBContext()
Return the JAXBContext. The JAXBContext represents the Java class to XML schema information for the POJOs.

getType

public Type getType(java.lang.Class entityClass)
Return the SDO type corresponding to the wrapped class.
 Type customerType = jaxbHelperContext.getType(Customer.class);
 DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);
 

getClass

public java.lang.Class getClass(Type type)
Return the wrapped class corresponding to the SDO type.
 Type customerType = jaxbHelperContext.getTypeHelper().getType("urn:customer", "customer");
 Class customerClass = jaxbHelperContext.getClass(customerType);
 

wrap

public DataObject wrap(java.lang.Object entity)
Return a DataObject that wraps a POJO. This call should be made on the root POJO.
 Customer customer = new Customer();
 Address address new Address();
 address.setStreet("123 Any Street");
 customer.set(address);
 
 DataObject customerDO = jaxbHelperContext.wrap(customer);
 customerDO.getString("address/street");  // returns "123 Any Street"
 
Multiple calls to wrap for the same instance POJO return the same instance of DataObject, in other words the following is always true:
 jaxbHelperContext.wrap(customer123) == jaxbHelperContext.wrap(customer123)
 jaxbHelperContext.wrap(customer123) != jaxbHelperContext.wrap(customer456)
 

wrap

DataObject wrap(java.lang.Object entity,
                Property containmentProperty,
                DataObject container)
Helper method that configures container information.

wrap

public java.util.List<DataObject> wrap(java.util.Collection<java.lang.Object> entities)
Perform the wrap operation on each of the POJOs in the collection, and return the results as a List.

wrap

java.util.List<DataObject> wrap(java.util.Collection<java.lang.Object> entities,
                                Property containmentProperty,
                                DataObject container)
Helper method that configures container information.

unwrap

public java.lang.Object unwrap(DataObject dataObject)
Return the POJO that is wrapped by the DataObject.
 Type customerType = jaxbHelperContext.getType(Customer.class);
 DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);
 DataObject addressDO = customerDO.create("address");
 addressDO.set("street", "123 Any Street");
 
 Customer customer = (Customer) jaxbHelperContext.unwrap(customerDO);
 customer.getAddress().getStreet();  // returns "123 Any Street"
 
Multiple calls to unwrap for the same DataObject must return the same instance of Object, in other words the following is always true:
 jaxbHelperContext.unwrap(customerDO123) == jaxbHelperContext.unwrap(customerDO123)
 jaxbHelperContext.unwrap(customerDO123) != jaxbHelperContext.unwrap(customerDO456)
 customer123 == jaxbHelperContext.unwrap(jaxbHelperContext.wrap(customer123))
 

unwrap

public java.util.List<java.lang.Object> unwrap(java.util.Collection<DataObject> dataObjects)
Perform the unwrap operation on each of the DataObjects in the collection, and return the results as a List.

putWrapperDataObject

void putWrapperDataObject(java.lang.Object anObject,
                          SDODataObject aDataObject)
Maintain an association between this POJO and DataObject.

getObjectDescriptor

XMLDescriptor getObjectDescriptor(SDOType sdoType)
Get the XML descriptor for the entity class corresponding to the SDO type.

Skip navigation links

Copyright © 1998, 2012, Oracle. All Rights Reserved.