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 8423401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:42:48+00:00 2026-06-10T03:42:48+00:00

First of all a small example. The class ReferencingEntity holds a reference to the

  • 0

First of all a small example. The class ReferencingEntity holds a reference to the abstract class AbstractEntity. There are two implementations fo this class:

@XmlRootElement
public abstract class AbstractEntity {

    @XmlID
    private String id;
}

@XmlRootElement
public class EntityImpl1 extends AbstractEntity {

}

@XmlRootElement
public class EntityImpl2 extends AbstractEntity {

}

@XmlRootElement
public class ReferencingEntity {

    @XmlIDREF
    private AbstractEntity entity;
}

There is no problem marshalling an instance of ReferencingEntity (except that the concrete type is not present in xml), but when trying to unmarshal the xml representation, the descriptor is missing to determine the concrete implementation.

Currently I’m using an XmlAdapter to set all non-id fields null, but it would be better to use @XmlID if possible. Any ideas?

UPDATE:
I’m using RESTEasy in JBoss 6.1.0.Final and the provider creates the context as follows:

ContextResolver<JAXBContextFinder> resolver = providers.getContextResolver(JAXBContextFinder.class, mediaType);
JAXBContextFinder finder = resolver.getContext(type);
if (finder == null)
{
   if (reader) throw new JAXBUnmarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
   else throw new JAXBMarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
}
JAXBContext context = finder.findCachedContext(type, mediaType, annotations);
  • 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-10T03:42:50+00:00Added an answer on June 10, 2026 at 3:42 am

    Below is my initial answer to your question. I imagine it will evolve as I better understand your use case.

    ABOUT @XmlID/@XmlIDREF

    Every instance referenced from a field/property annotated with @XmlIDREF also needs to be referenced via containment. I’ll use the class below in this example.

    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Root {
    
        private AbstractEntity abstractEntity;
        private ReferencingEntity referencingEntity;
    
        public AbstractEntity getAbstractEntity() {
            return abstractEntity;
        }
    
        public void setAbstractEntity(AbstractEntity abstractEntity) {
            this.abstractEntity = abstractEntity;
        }
    
        public ReferencingEntity getReferencingEntity() {
            return referencingEntity;
        }
    
        public void setReferencingEntity(ReferencingEntity referencingEntity) {
            this.referencingEntity = referencingEntity;
        }
    
    }
    

    REGARDING INHERITANCE

    JAXB (JSR-222) implementations can’t automatically discover subclasses, so you will need to be sure that the JAXBContext is aware of them. One way to accomplish this is to use the @XmlSeeAlso annotation on the parent class to point at the child classes.

    import javax.xml.bind.annotation.*;
    
    @XmlSeeAlso({EntityImpl1.class, EntityImpl2.class})
    @XmlAccessorType(XmlAccessType.FIELD)
    public abstract class AbstractEntity {
    
        @XmlID
        private String id;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
    }
    

    DEMO CODE

    Demo

    package forum12111815;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum12111815/input.xml");
            Root root = (Root) unmarshaller.unmarshal(xml);
    
            System.out.println(root.getAbstractEntity().getClass());
            System.out.println(root.getAbstractEntity() == root.getReferencingEntity().getEntity());
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    input.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
          <id>123</id>
       </abstractEntity>
       <referencingEntity>
          <entity>123</entity>
       </referencingEntity>
    </root>
    

    Output

    class forum12111815.EntityImpl2
    true
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
          <id>123</id>
       </abstractEntity>
       <referencingEntity>
          <entity>123</entity>
       </referencingEntity>
    </root>
    

    FOR MORE INFORMATION

    • http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
    • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all there is probably a question like this already but i couldn't
First of all, this isn't for a keylogger, it's for an input in a
First of all, apologize because I have seen some posts about this, but I
First of all I want to mention two things, One: My code isn't perfect
first of all some details: I configured security as below in web.xml view plaincopy
First of all, I'm quite new to the Android and JAVA world (coming from
first of all i would like to say i know its probably an easy
First of all I've gone through dozens of posting here on SO and google
First of all, I want to make something clear before I get yelled at:
At this moment I start work on small web application based on MVC. Now

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.