users@jaxb.java.net

Re: Set methods for collections

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Tue, 07 Feb 2006 12:54:38 +0100

Hi.

> 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.

You can find that out from JAXB's internal models. In JAXB 1, you could
check types of the FieldItem.

> So to review, my current issues regarding using JAXB 2 classes with
> hibernate 3 are.
>
> 1) XMLGregorianCalendar is not "native" to hibernate.

Use a custom type.

> 2) xsd:base64Binary is implemented with a char[] which I am not sure what
> hibernate will think of that.

I think it was byte[] earlier. Simply use a custom type (I have one in
Hyperjaxb2)

> 3) no set methods for collections...or what ever the problem is :)

Accessors.

> 4) no auto generation of binding files.

Well... ;) Someone has to do it.

> 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.

You can instruct Hibernate to use a special class when accessing the
property:

<list
   access="org.jvnet.hyperjaxb2.runtime.hibernate.accessor.ListAccessor"
   name="List1"
   table="ElementWithListAttribute1_List1">
   <!-- ... -->
</list>

Hibernate will use the provided list accessor to get/set the value.


> 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.

There's a lot more issues that you'll meet. I won't go with introspecion
since it provides too few details, not enough for the mapping generation.

Porting Hyperjaxb2 onto JAXB 2 is not too difficult. We only need to
replace ClassItem/FieldItem from JAXB 1 with JAXB 2 analogs. Then, if
anyone's interested, HBM file generation may be replaced with annotation
generation. However, my problem was that I could not find annotation
analog for all of the HBM constructs. For instance, a half a year ago it
was impossible to have a collection of simple types.

> 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 :)

Ok. I think I've already installed the hyperjaxb3 project on
dev.java.net some time ago. Feel free to join that.

Bye.
/lexi