Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Inheritance and JAXB work together?

public abstract class Parent<T> {
    protected List<T> list;

    @XmlTransient   //Question why do we have to give this here?
    public abstract List<T> getList();
    public abstract void setList(List<T> list);
}

@XmlRootElement(name = "child1")
class Child1 extends Parent<ExtendedElement1>{
    @Override
    public void setList(List<ExtendedElement1> list){
        this.list = list;
    }

    @Override
    @XmlElementWrapper(name = "child1-list")
    @XmlElement(name = "child-list-element")
    public List<ExtendedElement1> getList(){
        return this.list;
    }
}

@XmlRootElement(name = "child2")
class Child2 extends Parent<ExtendedElement2>{
    @Override
    public void setList(List<ExtendedElement2> list){
        this.list = list;
    }

    @Override
    @XmlElementWrapper(name = "child1-list")
    @XmlElement(name = "child-list-element")
    public List<ExtendedElement2> getList(){
        return this.list;
    }
}


class Element{
    @XmlElement(name = "integer", type = int.class)
    private int i = 2;
}

class ExtendedElement1 extends Element{
    @XmlElement(name = "extended-element1-str", type = String.class)
    private String str = "hello";
}

class ExtendedElement2 extends Element{
    @XmlElement(name = "extended-element2-str", type = String.class)
    private String str1 = "hello_there";
}

As I have shown in the example when I remove the @XmlTransient from the Parent class getList() method, following xml is marshalled:

<child1>
<!-- List is serialized 2 times -->
    <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
        <integer>2</integer>
        <extended-element1-str>hello</extended-element1-str>
    </list>
    <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
        <integer>2</integer>
        <extended-element1-str>hello</extended-element1-str>
    </list>
    <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
        <integer>2</integer>
        <extended-element1-str>hello</extended-element1-str>
    </list>
    <child1-list>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
    </child1-list>
</child1>

But when I add the @XmlTransient annotation, as in the example the xml is serialized with only ONE list as required.

<child1>
    <child1-list>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
        <child-list-element>
            <integer>2</integer>
            <extended-element1-str>hello</extended-element1-str>
        </child-list-element>
    </child1-list>
</child1>

So please can someone explain me why is it required to give @XmlTransient in Parent Class getter method? How does inheritance and JAXB inter-relate for these cases?

like image 388
Narendra Pathai Avatar asked Dec 18 '12 07:12

Narendra Pathai


People also ask

Does Jackson support type inheritance?

This tutorial has covered several interesting use cases to demonstrate Jackson's support for type inheritance, with a focus on polymorphism and ignorance of supertype properties. The implementation of all these examples and code snippets can be found in a GitHub project.

Why multiple inheritance is not supported by class in Java?

Multiple Inheritance is not supported by class because of ambiguity. In case of interface, there is no ambiguity because implementation to the method (s) is provided by the implementing class up to Java 7. From Java 8, interfaces also have implementations of methods.

What is interface inheritance in Java?

Interface inheritance : An Interface can extend other interface. An interface can also extend multiple interfaces. Why Multiple Inheritance is not supported through a class in Java, but it can be possible through the interface?

How does Jackson recreates data objects?

By default, Jackson recreates data objects by using no-arg constructors. This is inconvenient in some cases, such as when a class has non-default constructors and users have to write no-arg ones just to satisfy Jackson's requirements.


1 Answers

WHY ITS HAPPENING

A JAXB (JSR-222) implementation will map every domain object that it is aware of to a complex type. This means it believes that the following XML type exists for the Parent class (when list is not @XmlTransient).

  <xs:complexType name="parent" abstract="true">
    <xs:sequence>
      <xs:element name="list" type="xs:anyType" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

Now Child2 also has a complex type. There are two things JAXB could have done:

  • Not allow properties to be overridden, which would be pretty limiting.
  • Generate an additional mapping for the overridden property. In the schema below the child2 type extends parent. This means it gets all the elements from the parent type plus its own.
  <xs:complexType name="child2">
    <xs:complexContent>
      <xs:extension base="parent">
        <xs:sequence>
          <xs:element name="child1-list" minOccurs="0">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="child-list-element" type="extendedElement2" minOccurs="0" maxOccurs="unbounded"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>

HOW TO FIX IT

You could put @XmlTransient on the list property in the Parent class, but instead I would recommend annotating the Parent class with @XmlTransient.

import java.util.List;
import javax.xml.bind.annotation.XmlTransient;

@XmlTransient
public abstract class Parent<T> {
    protected List<T> list;

    public abstract List<T> getList();
    public abstract void setList(List<T> list);

}

This will remove it as a mapped class and the complex type that corresponds to Child2 will become:

  <xs:complexType name="child2">
    <xs:sequence>
      <xs:element name="child1-list" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="child-list-element" type="extendedElement2" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
like image 91
bdoughan Avatar answered Sep 22 '22 14:09

bdoughan