Below is my class and OXM mapping. Not sure why I am getting the below exception if I want the phoneType to be a child element under phone
public class PhoneNumber
{
public enum PhoneType
{
Cell, Home
};
private PhoneType phoneType;
private String type;
private String number;
public String getType()
{
return type;
}
public void setType(final String type)
{
this.type = type;
}
public String getNumber()
{
return number;
}
public void setNumber(final String number)
{
this.number = number;
}
public void setPhoneType(final PhoneType phoneType)
{
this.phoneType = phoneType;
}
public PhoneType getPhoneType()
{
return phoneType;
}
}
OXM
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
version="2.4" package-name="blog.bindingfile" xml-mapping-metadata-complete="true">
<xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
<java-types>
<java-type name="Customer">
<xml-root-element name="customer"/>
<xml-type prop-order="lastName firstName address phoneNumbers" />
<java-attributes>
<xml-element java-attribute="firstName" name="first-name" />
<xml-element java-attribute="lastName" name="last-name" />
<xml-element java-attribute="phoneNumbers" name="phone-number" />
</java-attributes>
</java-type>
<java-type name="BaseCustomer" xml-transient="true">
<xml-root-element/>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number" />
<xml-element java-attribute="phoneType" name="phone-type"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Exception
Caused by: Exception [EclipseLink-50010] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The property or field phoneType must be an attribute because another field or property is annotated with XmlValue.
at org.eclipse.persistence.exceptions.JAXBException.propertyOrFieldShouldBeAnAttribute(JAXBException.java:246)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:1011)
at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:412)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:102)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:768)
... 11 more
UPDATE
To fully understand why you are seeing the exception we will look at a formatted fragment of XML. The question comes down to which text fragment the @XmlValue mapping corresponds to (the first, second, neither?)
<phone-number type="TYPE">
<phone-type>Cell</phone-type>
</phone-number>
The following document involves mixed content, so JAXB makes you explicitly call this out. Normally mixed is used with @XmlAnyElement
so that get a list of everything elements and text so that order can be maintained.
<phone-number type="TYPE">
555-1111
<phone-type>Cell</phone-type>
</phone-number>
You are receiving the exception due to the following in your OXM file:
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number" />
<xml-element java-attribute="phoneType" name="phone-type"/>
</java-attributes>
</java-type>
As the exception states you are not allowed to map a field/property with @XmlElement
if another field/property in the class is mapped with @XmlValue
What you Need to Do Instead
You are really trying to map an element with mixed content. You can do so with the mapping file below:
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-any-element java-attribute="number" xml-mixed="true"/>
<xml-element java-attribute="phoneType" name="phone-type" />
</java-attributes>
</java-type>
FULL EXAMPLE
oxm.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11988974">
<java-types>
<java-type name="PhoneNumber">
<xml-root-element name="phone-number"/>
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-any-element java-attribute="number" xml-mixed="true"/>
<xml-element java-attribute="phoneType" name="phone-type" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Demo
package forum11988974;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
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, "forum11988974/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {PhoneNumber.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11988974/input.xml");
PhoneNumber phoneNumber = (PhoneNumber) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(phoneNumber, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<phone-number type="TYPE">555-1111<phone-type>Cell</phone-type></phone-number>