users@jaxb.java.net

Re: HibernateProxy causes class type mismatch

From: Levi Purvis <javanet_at_purvis.ws>
Date: Thu, 5 Jul 2007 22:59:11 -0400

Take a look at the XmlAccessorFactory / AccessorFactory extension
point in the latest release of JAXB 2.1.x. I submitted that patch for
that very reason (plus solving LazyInitializationException).

See:

com.sun.xml.bind.AccessorFactory
com.sun.xml.bind.XmlAccessorFactory

https://jaxb.dev.java.net/issues/show_bug.cgi?id=280

There's no documentation on this feature yet, so you'll have to take a
look at the source code and the testcase that I attached to the issue
above. The testcase has most of the code you'll need already written.

The gist of it is that you can plug in your own Accessor to grab a
Field or Property from a bean, but you get to inspect the value first
for a Hibernate proxy and "unwrap" it or "hide" it so that JAXB only
sees the "real" bean or null respectively. See:

Hibernate.isInitialized()
HibernateProxy.getHibernateLazyInitializer()
LazyInitializer.isUninitialized()
LazyInitializer.getImplementation()

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Hibernate.html
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/proxy/HibernateProxy.html
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/proxy/LazyInitializer.html


On 7/5/07, vr. sundar <vr.sundar_at_gmail.com> wrote:
> Hi,
>
> I posted this earlier on the jaxb forum but haven't got any response
> yet. So also sending it to the user mailing list ... The problem I
> face is this:
>
> When I retrieve an instance of a jaxb annotated class using hibernate,
> some of the child objects are represented by a hibernate proxy object.
> This causes a problem in marshalling the properties because the types
> of the objects don't match . For example if I have the following
> annotations (assuming e1, e2 are subtypes of e)
>
>
> XmlElements ({
> @XmlElement( name="a", type="a.b.c.d.e1.class")
> @XmlElement(name="b", type ="a.b.c.d.e2.class")
> })
> e someProperty;
>
> the type of the retrieved object is something like
> a.b.c.d.e1$$EnhancedByCGlib$$ and so it doesn't match the list of
> types for this property. This causes some weird behaviour. The tag
> names are different at diffferent runs. (In the jaxb source code this
> is in SingleElementNodeProperty.serializeBody(). It goes into the last
> (tt==null) block which has a comment saying this is acutally an error
> but then picks the first tagName from the iterator. )
>
> I'm wondering how to handle this problem. Is there a way for me to
> plug in something that lets me configure how the java types are
> matched?. Alternatively, can I do some kind of chaining of
> XmlJavaAdapterType? Maybe define an adapter for HibernateProxy which
> would then give out the actual object that can then be marshalled. I
> looked through the api docs but couldn't find anything for that. Looks
> like the output of the adapters have to be predefined pre-understood
> types probably the list that I saw in teh jaxb spec. Is that correct?
>
> Any help on this is greatly appreciated.
>
> thanks
> sundar
>
>
>