Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 367886

Summary: Issue with oxm.xml and @XmlAccessorType.NONE
Product: z_Archived Reporter: Blaise Doughan <blaise.doughan>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: matt.macivor
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Fix and test for Trunk
none
Fix and Test for 2.3 none

Description Blaise Doughan CLA 2012-01-04 14:50:44 EST
There is currently an issue with oxm.xml and @XmlAccessorType.NONE.  I will demonstrate the issue with the following example:

If you were going to map this use case with JAXB/MOXy annotations you could set `@XmlAccessorType(XmlAccessType.NONE)` on the `A` class and do something like:

**Using Annotations**

***A***

    package forum8727402;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.NONE)
    public abstract class A {
    
        public abstract String getX();
    
    }

***B***

    package forum8727402;
    
    import javax.xml.bind.annotation.*;    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    public class B extends A {
    
        @XmlPath("a/b/c/text()")
        private String x;
    
        public B() {
            x = "Hello World";
        }
    
        @Override
        public String getX() {
            return x;
        }
    
        @XmlElement
        public String getCalculatedValue() {
            return "Calculated Value";
        }
    
    }

***Demo***

    package forum8727402;
    
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(B.class);
    
            B b = new B();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(b, System.out);
        }
    
    }

***Output***

    <?xml version="1.0" encoding="UTF-8"?>
    <b>
       <a>
          <b>
             <c>Hello World</c>
          </b>
       </a>
       <calculatedValue>Calculated Value</calculatedValue>
    </b>

<hr/>

**Using MOXy's External Mapping File**

***oxm.xml***

Below is a [MOXy external mapping file][3] that represents the equivalent of the previously shown annotations:

    <?xml version="1.0" encoding="UTF-8"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum8727402">
        <java-types>
            <java-type name="A" xml-accessor-type="NONE"/>
            <java-type name="B">
                <xml-root-element/>
                <java-attributes>
                    <xml-element java-attribute="x" xml-path="a/b/c/text()"/>
                    <xml-element java-attribute="calculatedValue"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>

***Demo***

The code below demonstrates how to reference the mapping file:

    package forum8727402;
    
    import java.util.*;
    import javax.xml.bind.*;    
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8727402/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class, B.class}, properties);
    
            B b = new B();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(b, System.out);
        }
    
    }

***Output***

    [EL Warning]: 2012-01-04 14:45:46.366--Ignoring attribute [x] on class [forum8727402.xml.B] as no Property was generated for it.
    <?xml version="1.0" encoding="UTF-8"?>
    <b>
       <calculatedValue>Calculated Value</calculatedValue>
    </b>
Comment 1 Matt MacIvor CLA 2012-03-15 10:44:29 EDT
Created attachment 212727 [details]
Fix and test for Trunk
Comment 2 Matt MacIvor CLA 2012-03-15 11:19:26 EDT
Created attachment 212730 [details]
Fix and Test for 2.3
Comment 3 Matt MacIvor CLA 2012-03-15 14:20:32 EDT
Attached patches checked in to Trunk and 2.3
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:19:47 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink