Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/SDO/JAXB"

(Get the SDO Type for a POJO)
(Convert a POJO to a SDO DataObject)
Line 27: Line 27:
  
 
==Convert a POJO to a SDO DataObject==
 
==Convert a POJO to a SDO DataObject==
 +
POJOs corresponding to the types in the JAXBContext, can be converted to SDO DataObjects using the "wrap" operation on JAXBHelperContext.  This operation should be called on the root POJO.
 +
<pre>
 +
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"
 +
</pre>
 +
Multiple calls to wrap for the same instance POJO return the same instance of DataObject, in other words the following is always true:
 +
<pre>
 +
jaxbHelperContext.wrap(customer123) == jaxbHelperContext.wrap(customer123)
 +
jaxbHelperContext.wrap(customer123) != jaxbHelperContext.wrap(customer456)
 +
</pre>
  
 
==Convert a SDO DataObject to a POJO==
 
==Convert a SDO DataObject to a POJO==

Revision as of 11:03, 30 January 2009

Overview

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.

Create the JAXBHelperContext

The JAXBHelperContext is instantiated using a JAXBContext. This JAXBContext represents the object-to-XML mapping for the POJOs. If static DataObjects are being used, a second constructor is offered that takes a ClassLoader as a parameter.

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

For help on creating a JAXBContext see:

Define the SDO Metadata

An XML schema can be used to create the SDO metadata. This is the same schema that the POJOs are mapped to. This step has been separated so that SDO annotations could be added to the XML schema.

jaxbHelperContext.getXSDHelper().define(xmlSchema);

Get the SDO Type for a POJO

From the JAXBHelperContext you can determine the SDO type from a POJO class. This provides quick access to the necessary SDO metadata, to work with the data as SDO DataObjects.

Type customerType = jaxbHelperContext.getType(Customer.class);
DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);

Convert a POJO to a SDO DataObject

POJOs corresponding to the types in the JAXBContext, can be converted to SDO DataObjects using the "wrap" operation on JAXBHelperContext. This operation should be called 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)

Convert a SDO DataObject to a POJO