Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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/Examples/MOXy/Dynamic/StaticComparison"

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
(Next Steps)
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Overview==
 
==Overview==
 
In this example we will compare dynamic JAXB to the more familiar static form.  The main thing to note is that the use of JAXB is almost identical, the main difference is using the domain model.
 
In this example we will compare dynamic JAXB to the more familiar static form.  The main thing to note is that the use of JAXB is almost identical, the main difference is using the domain model.
 +
 +
EclipseLink Dynamic MOXy introduces a new concept in JAXB development - the freedom to bootstrap a JAXBContext from a variety of metadata sources and use familiar JAXB APIs to marshal and unmarshal data, all without having actual compiled domain classes. This enables the metadata to be updated without worrying about updating and recompiling the previously-generated Java source code.
 +
 +
Instead of actual Java classes (e.g. '''Customer''', '''Address''', etc), the domain objects used in Dynamic MOXy will be subclasses of '''DynamicEntity'''. DynamicEntities offer a simple get(propertyName) / set(propertyName, propertyValue) API to manipulate their data. DynamicEntities have an associated DynamicType, which will be generated in memory when the metadata is parsed.
  
 
==Static JAXB==
 
==Static JAXB==
Line 28: Line 32:
  
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set(street, "123 A Street");
+
address.set("street", "123 A Street");
address.set(city, "Any Town");
+
address.set("city", "Any Town");
 
customer.set("address", address);
 
customer.set("address", address);
  

Revision as of 14:52, 18 June 2012

Overview

In this example we will compare dynamic JAXB to the more familiar static form. The main thing to note is that the use of JAXB is almost identical, the main difference is using the domain model.

EclipseLink Dynamic MOXy introduces a new concept in JAXB development - the freedom to bootstrap a JAXBContext from a variety of metadata sources and use familiar JAXB APIs to marshal and unmarshal data, all without having actual compiled domain classes. This enables the metadata to be updated without worrying about updating and recompiling the previously-generated Java source code.

Instead of actual Java classes (e.g. Customer, Address, etc), the domain objects used in Dynamic MOXy will be subclasses of DynamicEntity. DynamicEntities offer a simple get(propertyName) / set(propertyName, propertyValue) API to manipulate their data. DynamicEntities have an associated DynamicType, which will be generated in memory when the metadata is parsed.

Static JAXB

We will bootstrap our JAXBContext on annotated classes generated from an XML schema. If the XML schema were to change we would need to regenerate the classes.

JAXBContext jaxbContext = JAXBContext.newInstance(org.example.Customer.class);
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(inputStream);
 
Address address = new Address();
address.setStreet("123 A Street");
address.setCity("Any Town");
customer.setAddress(address);
 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

Dynamic JAXB

Using dynamic JAXB we will bootstrap our JAXBContext on hte XML schema itself. If the XML schema were to change, we would not need to regenerate any classes.

DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set("street", "123 A Street");
address.set("city", "Any Town");
customer.set("address", address);
 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

Next Steps

Next we'll dig deeper into creating a Dynamic JAXB Context from XML Schema.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.