users@jaxb.java.net

Re: Set methods for collections

From: Lucas Jordan <lucasjordan_at_softhome.net>
Date: Mon, 06 Feb 2006 10:30:39 -0500

I will have a look at the accessor classes, thanks for the pointer.
I am ultimately interested in generating the hibernate binding files from
either the xsd files or the generated classes. I had some initial luck
creating simple binding files for classes, but I soon realized that I could
not deduce what classes are actually stored when the property accepts more
then a single value ( minOccurs="0" maxOccurs="unbounded"), this is a sort
coming of Java's introspection.


So to review, my current issues regarding using JAXB 2 classes with
hibernate 3 are.

1) XMLGregorianCalendar is not "native" to hibernate.
2) xsd:base64Binary is implemented with a char[] which I am not sure what
hibernate will think of that.
3) no set methods for collections...or what ever the problem is :)
4) no auto generation of binding files.

For 1 and 2 I have used the following in my xsd files:

<xsd:annotation>
        <xsd:appinfo>
            <jxb:globalBindings>
                <jxb:javaType name="java.lang.String"
xmlType="xsd:base64Binary"/>
                <jxb:javaType name="java.util.Calendar"
xmlType="xsd:dateTime"
                parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
                printMethod="javax.xml.bind.DatatypeConverter.printDate" />
            </jxb:globalBindings>
        </xsd:appinfo>
    </xsd:annotation>


For 3 it looks like I can "simply use an accessor", I will figure out what
exactly this means.

And for 4, I was initially looking to find a way for jaxb 2 to add an
annotation about what class is actually store in the collection. This would
allow me to creating binding files from the classes, since introspection
does not provide this information. I like the idea of creating the binding
files from the classes and not the xsd files because I do not have to worry
about customization. For example, I would have to keep track of the fact
that I am replacing XMLGregorianCalendar with GregorianCalendar.

I will take a look at hyperjaxb2 and the information about plug-ins and let
you all know if I decide to make a new plug-in, help update hyperjaxb2, or
just create the binding files by hand :)

Thanks for your help!
    -Lucas



On 2/5/06 4:22 PM, "Aleksei Valikov" <valikov_at_gmx.net> wrote:

> Hi.
>
>> I am trying to use the classes generated via JAXB 2 with hibernate. Is there
>> a property I can set to make JAXB generate setter methods for collections...
>>
>> So if we have:
>> <xsd:element name="Names" type="xsd:string" minOccurs="0"
>> maxOccurs="unbounded"/>
>>
>> We get the method:
>> public List<String> getNames();
>>
>> But hibernate also wants a:
>> public void getNames(List<String>);
>
> You don't need a set method for that, simply use an accessor. Take a
> look at org.jvnet.hyperjaxb2.runtime.hibernate.accessor package of
> Hyperjaxb2 (http://hyperjaxb2.dev.java.net), I've got appropriate
> classes there.
>
> You should also consider the following:
>
> 1. JAXB proxies lists with com.sun.xml.bind.util.ListImpl (at least in
> JAXB 1). And Hibernate checks collections by identity, so you have to be
> careful there in order not to get your collections updated too often.
>
> 2. Accessors also help to distinguish null/empty semantics of
> collections and primitive types. In JAXB, you may have isSetMyField()
> methods, so accessor may check those field to find out whether it's null
> or empty collection (for collections) or null or zero (for primitive types).
>
>> Are there any other resources I should know about?
>
>> Also, I currently am using a small set of of global bindings to make
>> the classes hibernate friendly, like using GregorianCalendar and not
>> XMLGregorianCalendar...is this something could accomplish? I ask
>> because a general use "make hibernate friendly" plug in might be nice
>> for others.
>
> Well, there's Hyperjaxb2 for JAXB1 which generates Hibernate mappings
> for JAXB classes. See http://hyperjaxb2.dev.java.net. Also see the
> reference at
> https://hyperjaxb2.dev.java.net/nonav/reference/html_single/index.html.
>
> Hyperjaxb2 incorporates A LOT of conceptual work to resolve numerous
> problems with Hibernate mapping generation. So you can actually learn a
> lot from this project.
>
> Anyway, I also have an intention to port Hyperjaxb2 onto JAXB2/Java5.
> Unfortunatelly, at the moment I don't have resources to do this. Too
> busy with my commercial projects. If anyone would be interested in
> implementing this port (actually, it's not too difficult), I'll provide
> my assistance.
>
> Bye.
> /lexi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>