Skip to main content

JAXB IDREF

1 reply [Last post]
nman
Offline
Joined: 2012-01-19
Points: 0

Hi everybody,

I have the a problem with JAXB marshalling of IDREFs

The classes I use:

It is possible, that firstList and secondList have references to the same object. To ensure that objects are not marshalled twice, I have to reference them with IDREF. My Problem is, that the marshalling doesnt work.

Here the code:

@XmlRootElement
@XmlSeeAlso({
B.class,
})
public abstract class A {
@XmlIDREF
@XmlElement
public List<C> firstList;
@XmlIDREF
@XmlElement
public List<C> secondList;
}
@XmlRootElement
public class B extends A {
}
@XmlSeeAlso({
D.class,
})
public abstract class C {
@XmlID
public String id = UUID.randomUUID().toString();
}
public class D extends C {
}
public static void main(String[] args) {
A aInstance = new B();
List<C> list = new ArrayList<C>();
List<C> list2 = new ArrayList<C>();
C dInstance = new D();
list.add(dInstance);
list2.add(dInstance);
aInstance.firstList = list;
aInstance.secondList = list2;
try{
Writer fw = new FileWriter("C:\\_temp\\a.xml");
JAXBContext context = JAXBContext.newInstance(A.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(aInstance,fw);
} catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
Thats the content of the output file:
<code></div>
<div><?xml version="1.0" encoding="UTF-8" standalone="yes"?></div>
<div><b></div>
<div>    <firstList>7d1261f2-2ef7-4a2e-92b9-cd36ffe8f90d</firstList></div>
<div>    <secondList>7d1261f2-2ef7-4a2e-92b9-cd36ffe8f90d</secondList></div>
<div></b></div>
<div></code>
So, the referenced objects were not marshalled and the lists are only containing the uuid's and no ref
Somebody knows the root of the problem and can help me?
Thx a lot,
Norman
AttachmentSize
problem.png4.84 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nman
Offline
Joined: 2012-01-19
Points: 0

Sorry, I dont know how to properly format code in this forum