Question

I have a set of a beans in a separate project that I'm unable to alter. These beans have both JPA and JAXB annotations, and are being used in a RESTful implementation. Most of my relationships are lazy-loaded, and I was hoping to achieve some more granular control over which elements are actually marshalled for transport.

I've got the modified MOXy Customer.java class below.

@javax.xml.bind.annotation.XmlType
@javax.xml.bind.annotation.XmlAccessorType(value=javax.xml.bind.annotation.XmlAccessType.PROPERTY)
public class Customer {

  private String name;
  private Address address;
  private List<PhoneNumber> phoneNumbers;

  // getters and setters
}

I was hoping I'd be able to use the MOXy eclipselink-oxm mapping to control what gets marshalled, but it isn't behaving as I'd expect. Using JAXB annotations, you declare an element (field or property) to be transient, but the eclipselink-oxm.xml only allows transient declarations on types. However, when I declare a type transient like so, I get the following exceptions:

<?xml version="1.0"?>
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
<java-types>
    <java-type name="example.gettingstarted.Customer">
        <xml-root-element/>
        <java-attributes>
            <xml-element java-attribute="name" xml-path="personal-info/name/text()"/>
            <xml-element java-attribute="address" xml-path="contact-info/address"/>
        </java-attributes>
    </java-type>

    <java-type name="example.gettingstarted.PhoneNumber" xml-transient="true" />

</java-types>
</xml-bindings>

Exception:

Exception [EclipseLink-110] (Eclipse Persistence Services - 2.1.0.v20100614-r7608):     org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [example.gettingstarted.PhoneNumber].
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[phoneNumbers]
Descriptor: XMLDescriptor(example.gettingstarted.Customer --> [DatabaseTable(customer)])

If I remove the xml-transient attribute, or set it to false, the Customer is transformed into XML as expected. Is there any way I can suppress the marshalling of phone numbers without modifying the Customer bean?

Was it helpful?

Solution

You can specify use the following mapping file to make the "phoneNumbers" property on Customer transient:

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <java-types>
        <java-type name="example.gettingstarted.Customer">
            <xml-root-element />
            <java-attributes>
                <xml-element java-attribute="name" xml-path="personal-info/name/text()" />
                <xml-element java-attribute="address" xml-path="contact-info/address" />
                <xml-transient java-attribute="phoneNumbers"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

For more information on MOXy's XML mapping file see:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top