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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:38:39+00:00 2026-05-24T07:38:39+00:00

I am trying to use MOXy JAXB to serialize a class A which looks

  • 0

I am trying to use MOXy JAXB to serialize a class A which looks like:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }
}

The point is that the Foo objects in the “foos” fields are a superset of those in the fooBar map. Therefore I would like to “link” the key elements for the “fooBar” map to the corresponding elements in the “foos” list. I have tried this using the XmlID and XmlIDREF annotations:

@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
    private String xmlId;

    @XmlID
    @XmlAttribute
    public String getXmlId() {
        return xmlId;
    }

    public void setXmlId(String xmlId) {
        this.xmlId = xmlId;
    }
}


@XmlAccessorType(XmlAccessType.NONE)
public class Bar {
    // Some code...
}

Then in my XmlAdapter I have tried to use a XmlIDREF annotation on the adapted map entries’ foo object:

public class FooBarMapAdapter extends
        XmlAdapter<FooBarMapAdapter.FooBarMapType, Map<Foo, Bar>> {
    public static class FooBarMapType {
        public List<FooBarMapEntry> entries = new ArrayList<FooBarMapEntry>();
    }

    @XmlAccessorType(XmlAccessType.NONE)
    public static class FooBarMapEntry {
        private Foo foo;
        private Bar bar;

        @XmlIDREF
        @XmlAttribute
        public Foo getFoo() {
            return foo;
        }

        public void setFoo(Foo foo) {
            this.foo = foo;
        }

        @XmlElement
        public Bar getBar() {
            return bar;
        }

        public void setBar(Bar bar) {
            this.bar = bar;
        }
    }

    @Override
    public FooBarMapType marshal(Map<Foo, Bar> map) throws Exception {
        FooBarMapType fbmt = new FooBarMapType();
        for (Map.Entry<Foo, Bar> e : map.entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            fbmt.entries.add(entry);
        }
        return fbmt;
    }

    @Override
    public Map<Foo, Bar> unmarshal(FooBarMapType fbmt) throws Exception {
        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : fbmt.entries) {
            map.put(entry.getFoo(), entry.getBar());
        }
        return map;
    }
}

When marshaling the code above is working as expected and produces the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <fooBar>
      <entries foo="nr1">
         <bar/>
      </entries>
   </fooBar>
   <foos xmlId="nr1"/>
</a>

For testing unmarshal, I am using the following test-code:

public class Test {
    public static void main(String[] args) throws Exception {
        A a = new A();

        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        Foo foo = new Foo();
        foo.setXmlId("nr1");
        Bar bar = new Bar();
        map.put(foo, bar);
        a.setFooBar(map);
        a.setFoos(map.keySet());

        final File file = new File("test.xml");
        if (!file.exists())
            file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);

        JAXBContext jc = JAXBContext.newInstance(A.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(a, fos);

        FileInputStream fis = new FileInputStream(file);
        Unmarshaller um = jc.createUnmarshaller();
        A newA = (A) um.unmarshal(fis);

        System.out.println(newA.getFooBar());
    }
}

This code produces the (for me) unexpected result:

{null=test.moxy.Bar@373c0b53}

That is, the Foo object used as key in the map is null. If I change the map adapter and marshal the Foo object twice, instead of using an ID reference, I do not get this null pointer.

I have been able to find some posts about this on google using the JAXB-RI, where the problem could be solved writing an IDResolver as described at http://weblogs.java.net/blog/2005/08/15/pluggable-ididref-handling-jaxb-20. Unfortunately I have not been able to find any information about such a class in the MOXy JAXB JavaDoc.

Suggestion for workaround
From Blaise Doughan answer, I have realized that this is a bug in the MOXy implementation of JAXB. I have been able to make a (ugly) workaround for this bug. The idea is that instead of using a XMLAdapter the map is “converted” inside its defining class. The class A now looks like:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    // Due to a bug a XMLAdapter approch is not possible when using XmlIDREF.
    // The map is mapped by the wrapper method getXmlableFooBarMap.
    // @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }

    // // WORKAROUND FOR JAXB BUG /////
    private List<FooBarMapEntry> mapEntries;

    @XmlElement(name = "entry")
    public List<FooBarMapEntry> getXmlableFooBarMap() {
        this.mapEntries = new LinkedList<FooBarMapEntry>();
        if (getFooBar() == null)
            return mapEntries;

        for (Map.Entry<Foo, Bar> e : getFooBar().entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            mapEntries.add(entry);
        }

        return mapEntries;
    }

    public void setXmlableFooBarMap(List<FooBarMapEntry> entries) {
        this.mapEntries = entries;
    }

    public void transferFromListToMap() {
        fooBar = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : mapEntries) {
            fooBar.put(entry.getFoo(), entry.getBar());
        }
    }
}

After the unmarshal, the transferFromListToMap-method now needs to be called. So the following line should be added immediately after the reference to newA is obtained:

newA.transferFromListToMap();

Any suggestions for a nicer workaround / bug fix will be appreciated :).

  • 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-24T07:38:40+00:00Added an answer on May 24, 2026 at 7:38 am

    Note: I’m the EclipseLink JAXB (MOXy) lead.

    I have been able to confirm the issue that you are seeing:

    • https://bugs.eclipse.org/353596

    Why the Issue is Happening

    The issue is due to MOXy processing the XmlAdapter logic before it has processed the @XmlIDREF logic. MOXy does a single pass of the XML document, and the @XmlIDREF relationships are processed at the end to ensure that all of the referenced objects have been built (as the reference may precede the referenced object, as in this case).

    I will try to post a workaround to this issue, and you can track our progress on this issue using the above bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use an excpetion class which could provide location reference for XML parsing,
I am trying to use Eclipselink's MOXy. I put jaxb.properties file in the same
I'm trying use epoch time dates in my series data. The array looks like
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
Trying to use a DataGridView like the old VB6 FlexGrid, and add the coloumns
I'm trying use an object which wasn't available until SDK level 5. It seems
I am trying use filehelpers class builder but I am kinda confused on what
Im trying use a Java annotation in a Groovy class but have trouble to
I'm trying use jquery to remove a class from an element (not knowing ahead
I was trying use a set of filter functions to run the appropriate routine,

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.