users@jaxb.java.net

Re: Set methods for collections

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Sun, 05 Feb 2006 22:22:45 +0100

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