Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Overview/DynamicEntities"

m (Replacing page with 'See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/dynamic_jaxb001.htm')
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/dynamic_jaxb001.htm
|info=y
+
|toc=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
}}
+
=Static and Dynamic Entities=
+
 
+
There are two high-level ways to use EclipseLink JAXB; Using pre-existing Java classes (Static MOXy), or using EclipseLink-generated in-memory Java classes (Dynamic MOXy).
+
 
+
 
+
==Using Static MOXy==
+
 
+
The most common way to use EclipseLink JAXB is with existing Java classes, mapped to XML using Java annotations and/or EclipseLink OXM metadata.  These classes might be ones that you have written yourself, or they could be generated from an XML Schema using the XJC compiler tool.
+
 
+
Using this approach, you will be dealing with your actual domain objects when converting to and from XML.  The following example shows a simple Java class that can be used with JAXB:
+
 
+
<source lang="java">
+
import javax.xml.bind.annotation.XmlAttribute;
+
import javax.xml.bind.annotation.XmlRootElement;
+
 
+
@XmlRootElement
+
public class Customer {
+
  @XmlAttribute
+
  private long id;
+
 
+
  private String name;
+
 
+
  // ...
+
  // get() and set() methods
+
  // ...
+
}
+
</source>
+
 
+
{{tip||When using static classes with JAXB, you can take advantage of JAXB's defaulting behaviour and only annotate things which differ from the default.  For example, all fields on a Java class will default to being mapped to an XML element, so no annotation is needed on the 'name' field.  We want the 'id' field, however, to map to an XML attribute, so have annotated it as such.}}
+
 
+
 
+
The code below demonstrates how to unmarshal, modify, and marshal object using static JAXB:
+
 
+
<source lang="java">
+
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class, Address.class);
+
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
+
 
+
Address address = new Address();
+
address.setStreet("1001 Fleet St.");
+
+
customer.setAddress(address);
+
 
+
jaxbContext.createMarshaller().marshal(customer, System.out);
+
</source>
+
 
+
 
+
==Using Dynamic MOXy==
+
 
+
With EclipseLink Dynamic MOXy, you can bootstrap a '''JAXBContext''' from a variety of metadata sources and use existing JAXB APIs to marshal and unmarshal data… ''without having compiled Java class files on the classpath''. This allows you to alter the metadata as needed, without having to update and recompile the previously-generated Java source code.
+
 
+
You should consider using Dynamic MOXy when:
+
*You want EclipseLink to generate classes from an XML schema (XSD).
+
*You do not want to deal with concrete Java domain classes.
+
 
+
 
+
===Dynamic Entities===
+
 
+
Instead of using actual Java classes (such as '''Customer.class''' or '''Address.class'''), Dynamic MOXy uses a simple '''get(propertyName)'''/'''set(propertyName, propertyValue)''' API to manipulate data. EclipseLink generates (in memory) a '''DynamicType''' associated with each '''DynamicEntity'''.
+
 
+
{{tip||'''DynamicTypes''' are similar to Java classes; whereas '''DynamicEntities''' can be thought of as instances of a '''DynamicType'''.}}
+
 
+
 
+
The code below demonstrates how to unmarshal, modify, and marshal object using dynamic JAXB:
+
 
+
<source lang="java">
+
DynamicJAXBContext dynamicJAXBContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, myClassLoader, null);
+
DynamicEntity customer = (DynamicEntity) dynamicJAXBContext.createUnmarshaller().unmarshal(instanceDoc);
+
+
String lastName = customer.get("lastName");
+
List orders = customer.get("orders");
+
...
+
DynamicEntity address = dynamicJAXBContext.newDynamicEntity("mynamespace.Address");
+
address.set("street", "1001 Fleet St.");
+
+
customer.set("lastName", lastName + "Jr.");
+
customer.set("address", address);
+
 
+
dynamicJAXBContext.createMarshaller().marshal(customer, System.out);
+
</source>
+
 
+
{{Template:EclipseLink_Spec
+
|section=Appendix D: Binding XML Names to Java Identifiers
+
}}
+
 
+
 
+
===Creating Dynamic Entities===
+
 
+
To create and use Dynamic MOXy entities, create a '''JAXBContext''' by using the '''DynamicJAXBContextFactory''' class. You can create a '''DynamicJAXBContext''' from an XML Schema file (XSD), EclipseLink OXM metadata file, or from an EclipseLink Project specified in the EclipseLink '''sessions.xml''' file.
+
 
+
*[[UserGuide/MOXy/Runtime/Bootstrapping/From_Schema|Bootstrapping from XML schema (XSD)]]
+
* Bootstrapping from EclipseLink OXM metadata
+
** [[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_OXM_using_DynamicEntities|Single project]]
+
** [[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_OXM_using_DynamicEntities|Multiple projects]]
+
* Bootstrapping from EclipseLink project (sessions.xml)
+
**[[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_sessions.xml_using_DynamicEntities|Single project]]
+
**[[EclipseLink/MOXy/Runtime/Bootstrapping/Single_Project/From_sessions.xml_using_DynamicEntities|Multiple projects]]
+
 
+
 
+
{{EclipseLink_MOXy
+
|next=[[EclipseLink/UserGuide/MOXy/Runtime|Runtime]]
+
|previous=[[EclipseLink/UserGuide/MOXy/Overview/Annotations|Annotations]]
+
|up=[[EclipseLink/UserGuide/MOXy/Overview|Overview]]
+
|version=2.2.0 Draft}}
+

Latest revision as of 10:35, 8 November 2012

See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/dynamic_jaxb001.htm