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

  • Home
  • SEARCH
  • 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 7797087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:34:20+00:00 2026-06-01T23:34:20+00:00

I wrote a bean ( BaseBeanEx ) extending a JAXB annotated bean ( BaseBean

  • 0

I wrote a bean (BaseBeanEx) extending a JAXB annotated bean (BaseBean). The BaseBean is in a List somewhere in the datastructure and can’t be changed. The Software does an explicit cast to BaseBeanEx whenever it is needed. I also wrote an ObjectFactory to create BaseBeanEx instead of BaseBean. This all works fine, but now I added a afterUnmarshal method to BaseBeanEx which never gets called.

Is this a bug or is this according to the specs? If later is the case, is there some elegant work around?

I’m using the default JAXB engine.

  • 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-06-01T23:34:23+00:00Added an answer on June 1, 2026 at 11:34 pm

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

    The reason that afterUnmarshal is not being called on BaseBeanEx is that the metadata was built on the BaseBean class. To get your use case to work you need to let your JAXB impl know that you really want to map to instances of BaseBeanEx.

    OPTION #1 – Any JAXB Implementation using Annotations

    Root

    You can use the @XmlElement annotation to override the type of a field/property. In the example below the signature of the method is List<BaseBean>, but the @XmlElement annotation informs the JAXB implementation the property should be interpreted as List<BaseBeanEx>.

    package forum10174513;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<BaseBean> baseBeans;
    
        @XmlElement(name="base-bean", type=BaseBeanEx.class)
        public List<BaseBean> getBaseBeans() {
            return baseBeans;
        }
    
        public void setBaseBeans(List<BaseBean> baseBeans) {
            this.baseBeans = baseBeans;
        }
    
    }
    

    OPTION #2 – Using MOXy’s External Mapping Document

    The BaseBean is in a List somewhere in the datastructure and can’t be
    changed.

    If you can’t modify your domain model and are using MOXy as your JAXB provider then you can leverage its external mapping document to apply metadata without modifying your domain model.

    bindings.xml

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum10174513">
        <java-types>
            <java-type name="Root">
                <java-attributes>
                    <xml-element 
                        java-attribute="baseBeans"
                        name="base-bean" 
                        type="forum10174513.BaseBeanEx"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo

    Below is some code that demonstrates how to bootstrap a JAXBContext that leverages the external mapping document. There is currently a bug where classes only referenced through the external mapping document won’t have there event methods registered (http://bugs.eclipse.org/376876). You can work around this issue by explicitly including this class in the list of classes used to create the JAXBContext.

    package forum10174513;
    
    import java.io.File;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    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, "forum10174513/bindings.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class, BaseBeanEx.class}, properties);
    
            File xml = new File("src/forum10174513/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Root root = (Root) unmarshaller.unmarshal(xml);
        }
    
    }
    

    BaseBean

    package forum10174513;
    
    public class BaseBean {
    }
    

    BaseBeanEx

    package forum10174513;
    
    import javax.xml.bind.Unmarshaller;
    
    public class BaseBeanEx extends BaseBean {
    
        public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
            System.out.println("AFTER UNMARSHAL WAS CALLED");
        }
    
    }
    

    Output

    Below is the output that was generated by running the demo code.

    AFTER UNMARSHAL WAS CALLED
    AFTER UNMARSHAL WAS CALLED
    

    For More Information

    • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
    • http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.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

How can I expose a Java bean to a JSP page by using struts?
I try to do that, but it does not work. <logic:present name=MEMBER > <bean:message
I am getting a div/list in my jsp using javascript; so i can pass
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a custom URL rewriting module, to take certain paths and map them
I wrote a small internal web app using (a subset of) pylons . As
I wrote a program that forks some processes with fork(). I want to kill
I wrote a routine to remove pounds and ids from sharepoint fields that worked
I wrote my code using this article at msdn as a primary helper My
I wrote an application using ASP.NET MVC, in this application I have an Index

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.