users@jaxb.java.net

Re: jaxb annotation in xsd file

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 24 Sep 2008 17:13:06 +0200

OK, from this (and the discussion between you and Felipe) I think I know
what your problem is.

First, let me say that XmlSeeAlso is just one more way of enhancing a
JAXBContext, which must know about all the Java classes partaking in
marshalling or unmarshalling. Usually this is used
in hand-written Java classes to save you lengthy argument lists when calling
JAXBContext.newInstance( Class... classes ). Since you, however, generate
everything from schema files, you'll most likely call newInstance with a
package path, with the classes you'd presumably specify in an XmlSeeAlso
being in some package anyway, so that their appearance in such an annotation
would be redundant.

Next, neither the possiblility of creating a Java binding for some XML
schema, nor JAXB's capability of deriving an XML structure from a set of
Java classes means that the XML schema language is object oriented, in the
sense that Java is. The fact that some xsd:simpleType or xsd:complexType
name="OtherType" is constructed using xsd:extension base="SomeType" doesn't
imply that all the elements where type="SomeType" is used may actually
contain some object of OtherType.

If JAXB (or any other XML serializer) would marshal (serialize) according to
the actual sub-type of some node in a content tree where the schema defines
the element's type as the base type would create non-validating XML data.
And, even if marshalling were allowed, parsing would be a nightmare, with
any of a multitude of subclasses to be detected, starting with a single tag.

To achieve the XML schema equivalent of a Java object of type ? extends
BaseType, you'll have to resort to xsd:choice, and specify all of your
alternatives.

Regards
Wolfgang


On 9/24/08, Maciej Zubala <maciej.zubala_at_gmail.com> wrote:
>
>
>
> 2008/9/23 Wolfgang Laun <wolfgang.laun_at_gmail.com>
>
>> There is no way to achieve this with annotations. If you need additional
>> subclasses of classes derived from your schema, why don't you extend the
>> schema?
>>
>> If you describe your original problem, perhaps there is a better solution.
>>
>>
>> On 9/23/08, Maciej Zubala <maciej.zubala_at_gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> I've got problems with inheritance in jaxb and I find on some forums that
>>> to solve my problem I need to annotate one class with XMLSeeAlso annotation.
>>> Unfortunatelly I cannot do it in java code generated from xsd, so here is my
>>> question: is it posible to put this annotation in xsd file so that xjc will
>>> annotate a java class automatically for me ??
>>>
>>
> This is rather long story but I'll try to make it short ;)
> I've got complicated hierarchy of types defined in xsd many (each type has
> lots of attributes and always inherits from some other type). XSD files are
> spread around many maven projects and I use maven plugins to generate java
> classes from xsd.
> These types are used as params in web services methods and here the problem
> starts. I've got some method that takes a parameter of type X. Type Y
> inherits from X so I can pass an instance of Y to this method.
> Unfortuyantely my web service client in this case only serializes attributes
> defined in X while it should also serialize attributes defined in Y as
> passed object is instance of Y.
> I've read somewhere out there that to solve this it's enough to annotate X
> as follows:
>
> XMLSeeAlso(value={Y.class})
>
> XJC in this particular case does not place this annotation itself ;/
> Unfortunatelly I cannot do it cause in my case X and Y are defined in
> separate maven projects. Project where Y is defined depends on project where
> X is defined. To place above annotation I would have to make prohect with X
> depend on project wit Y and I would get cyclic dependency.
> That is why I was wondering if it is possible to put that annotation in xsd
> so I don't have to care about dependencies.
>
> If you know any possible solution or have any suggestion please share it
> with me.
> I hope I explained everything clear.
>