users@jaxb.java.net

Re: jaxb2 and inheritance

From: Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>
Date: Tue, 06 Dec 2005 10:11:00 -0800

How are you creating a JAXBContext? I suspect that the JAXBContext
doesn't know about your IntegerProperty class. If it only knows about
Property, it tries to handle every Property-derived class as a Property,
hence it will only marshal the description.

If so, add IntegerProperty.class as an argument to JAXBContext.newInstance

Alexander Grünewald wrote:
> Hi Jaxb-Experts,
>
> i am testing jaxb2 and found it very useful, but i am wondering on the
> behaviour of jaxb2 when serializing an inheritance relationship. In my
> case I have three classes:
>
> //---------------------------
>
> @XmlType
> public abstract class Property {
> private String description;
> public String getDescription() { return description; }
> public void setDescription(String description) { this.description =
> description;}
> }
>
> //---------------------------
>
> @XmlType
> public class IntegerProperty extends Property {
> private int value;
> public int getValue() { return value;}
> public void setValue(int value) { this.value=value;}
> }
>
> //-------------------------------
>
> import java.util.*;
>
> @XmlRootElement
> public class PropertyList {
>
> private List<Property> properties = new ArrayList<Property>();
> public void setProperties(List<Property> properties) { this.properties
> = properties;}
> public List<Property> getProperties() { return properties; }
> }
>
> //-------------------------------
>
> My problem is the following: When I create an instance of
> 'PropertyList', add several 'IntegerProperty' instances to it's
> properties-List and write it to a XML file, the XML file does NOT
> contain the integer values, ... only the description which where
> inherited from the superclass 'Property'. I would expect, that the
> integer values where also written to XML.



-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com