Difference between revisions of "EclipseLink/UserGuide/MOXy/Advanced Concepts/Refreshing Metadata"

From Eclipsepedia

Jump to: navigation, search
m (Replacing page with 'See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts001.htm')
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts001.htm
|info=y
+
|toc=y
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/JAXBHelper.html JAXBHelper]
+
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/JAXBContext.html JAXBContext (EclipseLink)]
+
}}
+
 
+
= Refreshing Metadata =
+
 
+
EclipseLink MOXy '''2.3''' introduces the ability to refresh your '''JAXBContext's''' metadata at runtime.  This allows you to make changes to existing mappings in a live application environment and see those changes immediately without having to create a new '''JAXBContext'''.
+
 
+
In order to use the Metadata Refresh feature, your metadata information must be provided in one of the following formats:
+
 
+
* '''javax.xml.transform.Source'''
+
* '''org.w3c.dom.Node'''
+
* '''org.eclipse.persistence.jaxb.metadata.MetadataSource'''
+
 
+
 
+
== Example ==
+
 
+
Our example will be bootstrapped from the following EclipseLink OXM file:
+
 
+
<source lang="xml">
+
<?xml version="1.0"?>
+
<xml-bindings
+
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
+
    package-name="example">
+
    <java-types>
+
        <java-type name="Root">
+
            <java-attributes>
+
                <xml-element java-attribute="name" name="orig-name"/>
+
            </java-attributes>
+
        </java-type>
+
    </java-types>
+
</xml-bindings>
+
</source>
+
 
+
Our '''JAXBContext''' is created in the standard way:
+
 
+
<source lang="java">
+
...
+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
+
 
+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
dbf.setNamespaceAware(true);
+
DocumentBuilder db = dbf.newDocumentBuilder();
+
InputStream metadataStream = classLoader.getResourceAsStream("example/eclipselink-oxm.xml";
+
Document metadataDocument = db.parse(metadataStream);
+
metadataStream.close();
+
 
+
Map<String, Object> props = new HashMap<String, Object>(1);
+
props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataDocument);
+
JAXBContext context = JAXBContextFactory.createContext(new Class[] { Root.class }, props);
+
...
+
</source>
+
 
+
At this point, if we were to marshal a '''Root''' object to XML, it would look like this:
+
 
+
<source lang="xml">
+
<root>
+
    <orig-name>RootName</orig-name>
+
</root>
+
</source>
+
 
+
For this example, we will modify the metadata '''Document''' directly to change the XML name for the '''name''' field.  We can then refresh the metadata using the '''refreshMetadata()''' API:
+
 
+
<source lang="java">
+
...
+
Element xmlElementElement = (Element) metadataDocument.getElementsByTagNameNS("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element").item(0);
+
xmlElementElement.setAttribute("name", "new-name");
+
JAXBHelper.getJAXBContext(jc).refreshMetadata();
+
...
+
</source>
+
 
+
After refreshing metadata, the same '''Root''' object will be marshalled as follows:
+
 
+
<source lang="xml">
+
<root>
+
    <new-name>RootName</new-name>
+
</root>
+
</source>
+

Latest revision as of 10:29, 8 November 2012

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