Why can't I override the "type" property to be non-transient using external mapping as shown below? When I serialize, I doesn't see the "type" element.
public class PhoneNumber
{
private String type;
@XmlTransient
public String getType()
{
return type;
}
//other properties
}
I specified "type" using "xml-attribute" hoping that this will take precedence over the annotation but it isn't working.
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number" />
</java-attributes>
</java-type>
You appear to have hit a bug. You can track our progress on this issue using the link below:
WORKAROUND
You could specify that field access should be used for the PhoneNumber
class.
<java-type name="PhoneNumber" xml-accessor-type="FIELD">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number"/>
</java-attributes>
</java-type>
FULL EXAMPLE
PhoneNumber
package forum11991936;
import javax.xml.bind.annotation.XmlTransient;
public class PhoneNumber {
private String type;
private String number;
@XmlTransient
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
oxm.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11991936">
<java-types>
<java-type name="PhoneNumber" xml-accessor-type="FIELD">
<xml-root-element name="phone-number"/>
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum11991936;
import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11991936/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {PhoneNumber.class}, properties);
PhoneNumber pn = new PhoneNumber();
pn.setType("cell");
pn.setNumber("555-1111");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pn, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<phone-number type="cell">555-1111</phone-number>