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";
}
クラスメソッドからを削除すると、例で示したように、次の xml がマーシャリングされます。@XmlTransient
Parent
getList()
<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>
しかし、例のように注釈を追加すると、必要に応じてxmlが 1 つのリストだけでシリアル化されます。@XmlTransient
<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>
@XmlTransient
親クラスのゲッターメソッドで与える必要がある理由を誰かに説明してもらえますか? これらの場合、継承と JAXB はどのように相互に関連していますか?