Reputation: 15700
Is it possible, using JAXB, probably using MOXy, to “flatten” a base class into its subclass in marshalling, so that the Java inheritance is not visible in the XML? We have many hand-created classes that are based 1-to-1 on generated classes – the base class has no value in the XML.
If it isn’t obvious, we’re using the schemagen feature – starting with the Java, creating a schema.
Upvotes: 1
Views: 2046
Reputation: 149057
You can mark the base class with @XmlTransient.
@XmlTransient
public class Root {
}
This will cause the Child class to ignore the inheritance (WRT JAXB):
public class Child extends Root {
}
For other examples see:
Follow Up Issue
The issue you posted on the forum is a bug. You can workaround it using a binding file like the following:
binding-a.xml
In the binding file specify a type name for the transient class. This type will not appear in the generated XML schema:
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="example.a">
<java-types>
<java-type name="MyOwnGrandpa" xml-transient="true">
<xml-type name="MyOwnGrandpa2"/>
</java-type>
</java-types>
</xml-bindings>
example.a.MyOwnGrandpa
package example.a;
public class MyOwnGrandpa {
}
example.b.MyOwnGrandpa
package example.b;
public class MyOwnGrandpa extends example.a.MyOwnGrandpa {
}
example.Demo
package example;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import example.b.MyOwnGrandpa;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new File("src/exanmple/a/binding-a.xml"));
JAXBContext jc = JAXBContext.newInstance(new Class[] {MyOwnGrandpa.class} , properties);
jc.generateSchema(new MySOR());
}
private static class MySOR extends SchemaOutputResolver {
@Override
public Result createOutput(String arg0, String arg1) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(arg1);
return result;
}
}
}
Generated Schema
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="myOwnGrandpa"/>
</xsd:schema>
UPDATE
This issue is also being discussed on the EclipseLink Forum:
Upvotes: 2