How does MOXy xml transition declaration work?

I have a beans set in a separate project that I cannot change. These beans have JPA and JAXB annotations and are used in the RESTful implementation. Most of my relationships are lazy loaded, and I was hoping to get more control over what items are actually going to be transported.

I have a 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 could use the MOXy eclipselink-oxm mapping to control what it receives, but it does not behave as I expected. Using JAXB annotations, you declare an element (field or property) transient, but eclipselink-oxm.xml only allows transient declarations by type. However, when I declare a transition type this way, 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> 

An 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 client will be converted to XML as expected. Is there a way to suppress the sorting of phone numbers without changing the bean client?

+4
source share
1 answer

You can specify to use the following mapping file so that the "phoneNumbers" property is in the clientโ€™s successor:

 <?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 the MOXy XML mapping file, see below:

+2
source

Source: https://habr.com/ru/post/1345551/


All Articles