JAXB @XmlIDREF in XmlAdapter for immutable objects
I am using XmlAdapter
for immutable objects as suggested in this blog post: http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html . This works great, but not with references to other immutable objects in my adapters. Is there a way to handle this with JAXB?
Below is an example that doesn't work if the person's xml tag appears after the company xml tag that links to the person.
Immutable objects:
@XmlJavaTypeAdapter(PersonAdapter.class)
public class Person {
private final String id;
private final String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
@XmlJavaTypeAdapter(CompanyAdapter.class)
public class Company {
private final String name;
private final Person principal;
public Company(String name, Person principal) {
this.name = name;
this.principal = principal;
}
public String getName() {
return name;
}
public Person getPrincipal() {
return principal;
}
}
PersonAdapter:
public class PersonAdapter extends XmlAdapter<AdaptedPerson, Person> {
public static class AdaptedPerson {
@XmlID
@XmlAttribute
String id;
@XmlAttribute
String name;
}
@Override
public AdaptedPerson marshal(Person v) throws Exception {
AdaptedPerson a = new AdaptedPerson();
a.id = v.getId();
a.name = v.getName();
return a;
}
@Override
public Person unmarshal(AdaptedPerson v) throws Exception {
return new Person(v.id, v.name);
}
}
CompanyAdapter:
public class CompanyAdapter extends XmlAdapter<AdaptedCompany, Company> {
public static class AdaptedCompany {
@XmlAttribute
String name;
@XmlIDREF
@XmlAttribute
Person principal;
}
@Override
public AdaptedCompany marshal(Company v) throws Exception {
AdaptedCompany a = new AdaptedCompany();
a.name = v.getName();
a.principal = v.getPrincipal();
return a;
}
@Override
public Company unmarshal(AdaptedCompany v) throws Exception {
return new Company(v.name, v.principal);
}
}
+3
source to share
No one has answered this question yet
Check out similar questions: