Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:50:42+00:00 2026-05-28T04:50:42+00:00

Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML: abstract class MyAbstract

  • 0

Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML:

abstract class MyAbstract {
}

class MyImpl extends MyAbstract {
}

class A {

    private MyAbstract myAbstract;

    // MyImpl is behind this
    public MyAbstract getMyAbstract() {
        return myAbstract;
    }

}

I have following mapping defined in oxm.xml:

<java-type name="foo.MyAbstract" xml-accessor-type="NONE">
    <xml-see-also>
        foo.MyImpl
    </xml-see-also>
</java-type>

<java-type name="foo.MyImpl">
    <xml-root-element name="MyImpl" />
</java-type>

<java-type name="bar.A" xml-accessor-type="NONE">
    <xml-root-element name="A" />
    <java-attributes>
        <xml-element java-attribute="myAbstract" type="foo.MyAbstract" />
    </java-attributes>
</java-type>

Now this results in:

<A>
    <myAbstract xsi:type="myImpl">
        <!-- Mapped members of MyImpl + MyAbstract -->
    </myAbstract>
</A>

Since i didnt want the property-name in the exported xml I changed:

<java-type name="bar.A" xml-accessor-type="NONE">
    <xml-root-element name="A" />
    <java-attributes>
        <xml-element java-attribute="myAbstract" type="foo.MyAbstract" xml-path="."/>
    </java-attributes>
</java-type>

which resulted in:

<A>
    <!-- Members of MyImpl + MyAbstract marshalled without any wrapping element-->
</A>

What i want is:

 <A>
    <MyImpl>
        <!-- Members of MyImpl + MyAbstract -->
    </MyImpl>
 </A>

The question is: how do i achieve this? MOXy is just ignoring my XmlRootElement on MyImpl…

EDIT:

Trying what Blaise suggested gives me following exception:

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):   
org.eclipse.persistence.exceptions.DescriptorException
The method [] or [getMyAbstract] is not defined in the object [bar.A].

Now this needs further information which i had left out before because i thought it was not relevant:

Class A is an interface which defines: public X getMyAbstract();
MyAbstract implements X (this is why i added the type-attribute in the mapping for interface A).

So, using xml-element-ref MOXy does not “see” the getter anymore, using xml-element it does.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:50:42+00:00Added an answer on May 28, 2026 at 4:50 am

    The mapping that you are looking for is @XmlElementRef. This corresponds to the concept of substitution groups in XML schema.

    bar/oxm.xml

    Below is the external mapping document for the bar package. Note how the myAbstract property is mapped with xml-element-ref which is the XML representation of @XmlElementRef

    <?xml version="1.0" encoding="UTF-8"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="bar">
        <java-types>
            <java-type name="A" xml-accessor-type="NONE">
                <xml-root-element name="A" />
                <java-attributes>
                    <xml-element-ref java-attribute="myAbstract"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    foo/oxm.xml

    Below is the external metadata file for the foo package:

    <?xml version="1.0" encoding="UTF-8"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="foo">
        <java-types>
            <java-type name="MyAbstract" xml-accessor-type="NONE">
                <xml-see-also>
                    foo.MyImpl
                </xml-see-also>
            </java-type>
            <java-type name="MyImpl">
                <xml-root-element name="MyImpl" />
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo

    Below is demo code for this example:

    package forum8853855;
    
    import java.util.*;
    import javax.xml.bind.*;    
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    import bar.A;
    import foo.MyImpl;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            List<String> oxm = new ArrayList<String>(2);
            oxm.add("foo/oxm.xml");
            oxm.add("bar/oxm.xml");
    
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
    
            JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class}, properties);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            A a = new A();
            a.setMyAbstract(new MyImpl());
            marshaller.marshal(a, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <A>
       <MyImpl/>
    </A>
    

    For More Information

    • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following use-case for marshalling a POJO to XML using Eclipselink MOXy 2.3:
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I am using eclipselink, and I have the following code to call a stored
Using EclipseLink 2.3.2 with static weaving (Maven plugin) against OracleXE. I have the following
I'm facing a marshalling/unmarshalling problem involving inheritance and polymorphism using MOXy's JAXB implementation and
We are using Eclipselink for ORM, but we have a need for some more
I'm using spring mvc 3.0 with eclipselink and jpa and I'm experiencing following issue:
Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?
I'm using EclipseLink in Glassfish with my JavaEE application and have some java.util.Locale-columns in
I'm using EclipseLink as the JPA provider. Further I'm using the following TABLE_PER_CLASS inheritance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.