DaHoopster
DaHoopster

Reputation: 602

JAXB 2 annotation and subclasses headache

I am currently stuck on a awkward problem on JAXB. So I have the following class structure in place:

@XmlType
public abstract class MySuperClass
{
    ...
    ...
    @XmlTransient
    public Double getValue() ...
    ...
}

@XmlType
public class MySubClass extends MySuperClass
{
    public Double getValue()
    {
        return 100.00;
    }
}

@XmlType
public class MySubClass2 extends MySuperClass
{
    public Double getValue()
    {
        return 100.00;
    }
}

And now in my other JAXB annotated class, I wish to do this:

@XmlType
public class MyOtherClass
{
    private MySuperClass var;

    public MySuperClass getVar()
    {
        return this.var;
    }
}

So the reason is that I would like to set var at run time so that the actual return type would be either MySubClass or MySubClass2. Marshalling to XML is flawless, however, unmarshalling back to java classes gave me null values. The setter method wasn't called. How can I unmarshal correctly and still maintain the class hierarchy?

Help is much appreciated ...

Thanks,

Upvotes: 1

Views: 789

Answers (1)

DaHoopster
DaHoopster

Reputation: 602

OK, got this one resolved. It seems JAXB is looking for the declared setter method within the class only and not going to the parent class. After overriding the setter method in the subclass, unmarshalling worked.

Upvotes: 1

Related Questions