users@jaxb.java.net

controlling inheritence/inlining classes

From: Andrew Geery <andrew.geery_at_gmail.com>
Date: Wed, 15 Aug 2012 08:32:03 -0400

When I use xjc on the schema fragment at the bottom of this message, I end
up with three classes: TopLevel, Sub1 which extends Sub2. I would like to
use the generated classes for JPA (in fact, I'm using hyperjaxb3 to
generate both the JPA and XML bindings). In order for this to work, I
really don't want the classes Sub1 and Sub2 generated; I want their
definitions to be rolled up into the TopLevel class (otherwise, I end up on
the JPA side of things with Sub1 being an @Embeddable class, but extending
Sub2 which isn't supported in JPA; plus, Sub1 has one-to-many relationships
with other entities which don't map well with an @Embeddable class). That
is, I really want a TopLevel class generated from this structure:

       <xsd:complexType name="TopLevel">
<xsd:sequence>
<!-- TopLevel elements -->
 <!-- Sub1 elements -->
                        <!-- Sub2 elements -->
</xsd:sequence>
 </xsd:complexType>

Is there any way to tell xjc, via the bindings.xml file, to in-line the
properties into a single class or to "flatten" the class hierarchy? Is
there an option in hyperjaxb3 to do this? My workaround is to transform
the schema before I run xjc, but I was wondering if there was a better way?

Thanks
Andrew

       <xsd:complexType name="TopLevel">
<xsd:sequence>
 <!-- other elements -->
<xsd:element ref="Sub1"/>
 </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Sub1">
 <xsd:complexContent>
<xsd:extension base="Sub2">
 <xsd:sequence>
<!-- other elements -->
</xsd:sequence>
 </xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="Sub2">
<xsd:complexContent>
 <xsd:sequence>
<!-- elements -->
</xsd:sequence>
 </xsd:complexContent>
</xsd:complexType>