Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXy: xml-mapping-metadata-complete problem
MOXy: xml-mapping-metadata-complete problem [message #789127] Thu, 02 February 2012 16:23 Go to next message
David Vree is currently offline David VreeFriend
Messages: 48
Registered: July 2010
Member
I have a particular JAXBContext that I am trying to establish that ignores all JAX-B annotations and only uses what mappings are in the OXM file. I have already set:

xml-mapping-metadata-complete="true"

However I am seeing that properies with JAX-B annotations that were inherited from superclasses are still showing up in the XML/JSON encoding. I would have expected that no JAXB annotations would have been honored.

The superclass is in a different package from my class. Do I need to provide an additional mapping file for the superclass's package?
Re: MOXy: xml-mapping-metadata-complete problem [message #789940 is a reply to message #789127] Fri, 03 February 2012 15:49 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

The MOXy external mapping document applies at the package level so you will need to have one per package. Below is an example.

feb3.foo.Foo

The following class represents the annotated super class in one package:

package feb3.foo;

import javax.xml.bind.annotation.XmlAttribute;

public class Foo {

    private String foo;

    @XmlAttribute
    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

}


feb3.bar.Bar

And this class represents the annotated subclass in another package.

package feb3.bar;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import feb3.foo.Foo;

@XmlRootElement
public class Bar extends Foo {

    private String bar;

    @XmlAttribute
    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}


feb3/foo/oxm.xml

We need one external mapping document to override the metadata in the feb3.foo package.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="feb3.foo" xml-mapping-metadata-complete="true"/>


feb3/bar/oxm.xml

And we need another external mapping document to override the metadata in the feb3.bar package.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="feb3.bar" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Bar">
            <xml-root-element/>
        </java-type>
    </java-types>
</xml-bindings>


feb3.demo.Demo

The following demo code demonstrates how to bootstrap a JAXBContext from multiple external mapping documents.

package feb3.demo;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import feb3.bar.Bar;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        List<String> oxm = new ArrayList<String>(2);
        oxm.add("feb3/foo/oxm.xml");
        oxm.add("feb3/bar/oxm.xml");
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Bar.class}, properties);

        Bar bar = new Bar();
        bar.setFoo("FOO");
        bar.setBar("BAR");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(bar, System.out);
    }

}


Output

The following is the output from running the demo code. Note how the @XmlAttribute annotations on both the Foo and Bar classes have been ignored:

<?xml version="1.0" encoding="UTF-8"?>
<bar>
   <foo>FOO</foo>
   <bar>BAR</bar>
</bar>


Uses of xml-mapping-metadata-complete="true"

This flag is useful when you want to apply a second mapping to your object model that is completely different from the current mapping. Below is an example where this is used to map one object model to both the Google and Yahoo weather APIs:

http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
Re: MOXy: xml-mapping-metadata-complete problem [message #789942 is a reply to message #789127] Fri, 03 February 2012 15:49 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

The MOXy external mapping document applies at the package level so you will need to have one per package. Below is an example.

feb3.foo.Foo

The following class represents the annotated super class in one package:

package feb3.foo;

import javax.xml.bind.annotation.XmlAttribute;

public class Foo {

private String foo;

@XmlAttribute
public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

}

feb3.bar.Bar

And this class represents the annotated subclass in another package.

package feb3.bar;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import feb3.foo.Foo;

@XmlRootElement
public class Bar extends Foo {

private String bar;

@XmlAttribute
public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

}

feb3/foo/oxm.xml

We need one external mapping document to override the metadata in the feb3.foo package.

<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.foo" xml-mapping-metadata-complete="true"/>

feb3/bar/oxm.xml

And we need another external mapping document to override the metadata in the feb3.bar package.

<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.bar" xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Bar">
<xml-root-element/>
</java-type>
</java-types>
</xml-bindings>

feb3.demo.Demo

The following demo code demonstrates how to bootstrap a JAXBContext from multiple external mapping documents.

package feb3.demo;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import feb3.bar.Bar;

public class Demo {

public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
List<String> oxm = new ArrayList<String>(2);
oxm.add("feb3/foo/oxm.xml");
oxm.add("feb3/bar/oxm.xml");
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Bar.class}, properties);

Bar bar = new Bar();
bar.setFoo("FOO");
bar.setBar("BAR");

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bar, System.out);
}

}

Output

The following is the output from running the demo code. Note how the @XmlAttribute annotations on both the Foo and Bar classes have been ignored:

<?xml version="1.0" encoding="UTF-8"?>
<bar>
<foo>FOO</foo>
<bar>BAR</bar>
</bar>

Uses of xml-mapping-metadata-complete="true"

This flag is useful when you want to apply a second mapping to your object model that is completely different from the current mapping. Below is an example where this is used to map one object model to both the Google and Yahoo weather APIs:

http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
Re: MOXy: xml-mapping-metadata-complete problem [message #790069 is a reply to message #789940] Fri, 03 February 2012 19:25 Go to previous messageGo to next message
David Vree is currently offline David VreeFriend
Messages: 48
Registered: July 2010
Member
That worked. Thanks. Is it possible to have two subclasses of a superclass override their superclass's mapping in different ways? Just curious...
Re: MOXy: xml-mapping-metadata-complete problem [message #790071 is a reply to message #789942] Fri, 03 February 2012 19:25 Go to previous message
David Vree is currently offline David VreeFriend
Messages: 48
Registered: July 2010
Member
That worked. Thanks. Is it possible to have two subclasses of a superclass override their superclass's mapping in different ways? Just curious...
Previous Topic:MOXy: &lt;xml-attribute&gt; and JSON
Next Topic:strange Persistence exception with a List type
Goto Forum:
  


Current Time: Tue Nov 12 13:12:46 GMT 2024

Powered by FUDForum. Page generated in 0.03424 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top