'Mapstruct with JAXB, @XmlElementRefs

I spent really a lot of time on this, but I can not find the answer. From documentation it seams it should work automatically, but for me it does not. I am facing following problem:

XSD:

   <xsd:complexType name="AnotherClass">
      <xsd:choice>
         <xsd:sequence maxOccurs="2">
            <xsd:element name="xmlName1" type="SomeClass">
            </xsd:element>
            <xsd:element name="xmlName2" type="SomeClass" minOccurs="0">
            </xsd:element>
         </xsd:sequence>
      </xsd:choice>
   </xsd:complexType>

   <xsd:complexType name="SomeClass">
      <xsd:sequence>
         <xsd:element name="value" type="xs:string">
      </xsd:sequence>
   </xsd:complexType>

Generated class:

    @XmlElementRefs({
        @XmlElementRef(name = "xmlName1", namespace = "http://com.znamenacek", type = JAXBElement.class),
        @XmlElementRef(name = "xmlName2", namespace = "http://com.znamenacek", type = JAXBElement.class)
    })
    protected List<JAXBElement<SomeClass>> xmlName1sAndXmlName2s;


    public List<JAXBElement<SomeClass>> getXmlName1sAndXmlName2s() {
        if (xmlName1sAndXmlName2s == null) {
            xmlName1sAndXmlName2s= new ArrayList<JAXBElement<SomeClass>>();
        }
        return this.xmlName1sAndXmlName2s;
    }

Factory Method:

    @XmlElementDecl(namespace = "http://com.znamenacek", name = "xmlName1", scope = AnotherClass.class)
    public JAXBElement<SomeClass> createAnotherClassXmlName1(SomeClass value) {
        return new JAXBElement<SomeClass>(QNAME, SomeClass.class, AnotherClass.class, value);
    }

Mapper:

    @Mapping(source = "source1.source2", target = "target1.target2.xmlName1.value")
    void updateFromSource(Source source, @MappingTarget Target target);

I would like to achieve, that value from source2 (String) will add the JAXBElement corresponding to xmlName1 to the xmlName1sAndXmlName2s list and set the value to value from source2.

Hopefully I described my problem clearly



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source