« Return to Thread: [MOXy] mapping inheritance of schema types

[MOXy] mapping inheritance of schema types

by polly.c.chang :: Rate this Message:

Reply to Author | View in Thread

Hi,

My schema defines the following inheritance:  

        <xsd:complexType name="RootType" abstract="true">
                <xsd:sequence>
                        <xsd:element ref="field1" minOccurs="0"/>
                </xsd:sequence>
        </xsd:complexType>

        <xsd:complexType name="BranchType" abstract="true">
                <xsd:complexContent>
                        <xsd:extension base="RootType">
                                <xsd:sequence>
                                        <xsd:element ref="field2" maxOccurs="unbounded"/>
                                </xsd:sequence>
                        </xsd:extension>
                </xsd:complexContent>
        </xsd:complexType>

        <xsd:complexType name="LeafType">
                <xsd:complexContent>
                        <xsd:extension base="BranchType">
                                        <xsd:element ref="field3" maxOccurs="unbounded"/>
                        </xsd:extension>
                </xsd:complexContent>
        </xsd:complexType>

The Java classes also mirror this inheritance tree:

public class Root {
    private String field1;
}

public class Branch extends Root {
    private String field2;
}

public class Leaf extends Branch {
    private String field3;
}

Actually, Root and Branch ought to be abstract classes, but EclipseLink threw instantiation exceptions complaining about missing no-arg constructor until I took out the "abstract".

I expect to parse XML that looks like this into the Leaf object:

<Leaf>
    <field1>...</field1>
    <field2>...</field2>
    <field3>...</field3>
</Leaf>

I don't want to redundantly map the fields from Branch and Root in Leaf, so I am trying to map each of those classes to its corresponding type in the schema.  In the Workbench, I have these settings:

1.  Leaf has a mapping for field3, the class is marked as a child descriptor, and its parent is Branch.  
2.  Branch has a mapping for field2, the class is marked as a child descriptor, and its parent is Root.  
3.  Root has a mapping for field1, the class is marked as a parent, and then... I'm stuck.

The problem is:
* I cannot use the Class Extraction Method, because it looks like this hook is never called for XML projects.

* I cannot use the Class Indicator Field's "xml schema type attribute", because I assume this means that I need an an attribute like "xsi:type='Leaf'", and my schema does not allow this attribute.

* I cannot use the Class Indicator Field's "specify field" setting because there's no element inside the <Leaf></Leaf> tags that will tell me what class to use.  

So I am perplexed.  How come EclipseLink can't tell that it should use the Leaf class if the xml element name is "Leaf"?  I have already set the "Schema Context" to "Leaf".  Why does EclipseLink need something else to tell it what type to use?  

Is there a better way to specify inheritance in XML mappings without having to use the Map Inherited Attributes -> To Root Minus One option?  That would force me to remap the same attributes over and over again and be a pain if the root definition ever changes.  

Thanks for your help,
Polly

 « Return to Thread: [MOXy] mapping inheritance of schema types