The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: Customizing marshalling

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 12 - Last Post: Jul 9, 2009 11:14 AM by: ming_chan
gadams00

Posts: 47
Customizing marshalling
Posted: May 22, 2006 1:23 PM
  Click to reply to this thread Reply

I'm trying to use developer-annotated classes with JAXB in conjunction with Hibernate 3. The problem I'm running into is an org.hibernate.LazyInitializationException when JAXB tries to marshal a bean contained as a reference inside another bean. Hibernate causes the reference to be to a proxy, which throws LazyInitializationException if the bean wasn't loaded from the database.

Basically, I need to call org.hibernate.Hibernate.isInitialized(obj) when marshalling and not marshal anything if isInitialized returns false. Is there a way to do this?

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: May 30, 2006 9:31 AM   in response to: gadams00
  Click to reply to this thread Reply

This is an interesting use case. But I'm not very familiar with Hibernate, so I need your help.

I'd like you to first experiment with a local modification to JAXB runtime. If you think it works good enough, then we can talk about incorporating into the RI, then eventually to the spec if possible.

Check XMLSerializer.childAsSoleContent. This methd is called most of the time to marshal a child object. You see that it does:

        if(child==null) {
            handleMissingObjectError(fieldName);
        } else {


Can you change it to:

        if(child==null) {
            handleMissingObjectError(fieldName);
        } else if(!Hibernate.isInitialized(child)) {
            // skip this child
        } else {


and see if that achieves what you'd like to do?

gadams00

Posts: 47
Re: Customizing marshalling
Posted: Jun 1, 2006 10:29 PM   in response to: kohsuke
  Click to reply to this thread Reply

I'm putting this on my todo list. I've got a large project to work on for now, but in a few days, I should be able to test this.

Adding this to the RI and/or spec would create an artificial dependency on the Hibernate API, which I think would be bad. Maybe there's some way to create a customization hook that would serve this purpose?

Greg

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: Jun 5, 2006 10:26 PM   in response to: gadams00
  Click to reply to this thread Reply

Yes, the first thing is to find out if this can be made to work.

Then we can figure out the right way to do it. It shouldn't be difficult to define abstractions to avoid dependency.

gadams00

Posts: 47
Re: Customizing marshalling
Posted: Jun 8, 2006 2:34 PM   in response to: kohsuke
  Click to reply to this thread Reply

Okay, I tried the suggested fix out and it did not work. What I needed to change was com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody

there's a line:
if(v!=null) {
Class vtype = v.getClass();
TagAndType tt=typeNames.get(vtype); // quick way that usually works
...


that I changed to:
if(v!=null && org.hibernate.Hibernate.isInitialized(v)) {
Class vtype = v.getClass();
TagAndType tt=typeNames.get(vtype); // quick way that usually works
...


Now the lazy-loaded proxied properties are handled as though they were null, which was what I needed.

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: Jun 9, 2006 4:53 PM   in response to: gadams00
  Click to reply to this thread Reply

Thanks!!

If you modify serializeBody, I suspect you'd also have to change other properties for the completeness. So it's a lot of places to make changes.

What if we write a filter Accessor that does the check? Would that work?

Would it be possible for you to share this prototype work with us? I'd like to do a few experiments, too, if possible, and I have a feeling that others might chime in.

gadams00

Posts: 47
Re: Customizing marshalling
Posted: Jun 12, 2006 9:19 AM   in response to: kohsuke
  Click to reply to this thread Reply

Sure, I've got the test program all zipped up. Where can I send it?

I'm not familiar with the 'filter Accessor' that you mentioned. How does that work?

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: Jun 12, 2006 8:06 PM   in response to: gadams00
  Click to reply to this thread Reply

You can send it to me: kohsuke.kawaguchi at sun dot com

If you search the source code for the class called "Accessor", you'll find the class that the JAXB RI uses to access values from fields.

The idea is to write an Accessor that mostly delegates to another "real" Accessor. That would let you check for uninitialzed objects and return null instead, causing the JAXB marshaller to believe that there was no such value in the first place.

lpurvis

Posts: 13
Re: Customizing marshalling
Posted: Nov 14, 2006 2:54 PM   in response to: kohsuke
  Click to reply to this thread Reply

I could really use this functionality as well! I'm hitting the same problems, also with lazy Collections.

I don't see a way to plug in custom "Accessor" implementations in 2.1 EA? Has any work been done on this in the nightly builds?

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: Nov 15, 2006 7:32 AM   in response to: lpurvis
  Click to reply to this thread Reply

No. We do need to add some hooks to the RI to make this happen, but that shouldn't be difficult.

Could you file an RFE for this? So that we can keep track of it?

lpurvis

Posts: 13
Re: Customizing marshalling
Posted: Nov 15, 2006 10:19 AM   in response to: kohsuke
  Click to reply to this thread Reply

> Could you file an RFE for this? So that we can keep
> track of it?

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

kohsuke

Posts: 3,962
Re: Customizing marshalling
Posted: Nov 15, 2006 9:48 PM   in response to: lpurvis
  Click to reply to this thread Reply

Thanks. I saw your e-mail in the dev alias, too.
I couldn't get to it today because of releases, but will get back to you soon.

ming_chan

Posts: 3
Re: Customizing marshalling
Posted: Jul 9, 2009 11:14 AM   in response to: gadams00
  Click to reply to this thread Reply

Hello,

I am using Hibernate 3 in glassfish v2.1 and I ran into this same problem. In my project there are stateless session beans exposing SOAP web service interface. These SLSBs access Hibernate entities and in many cases, returns one or Collection of these entities in the web service response.

During JAXB marshaling when constructing the web service response, if a entity (the one to be returned) has related entities via a lazy load association then it throws:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Just like the problem described here. Another twist is that the same problem was NOT there when the same project was deployed to glassfish v2.0.

Since this thread is more than 2 years old, do we have a more or less standard solution to tell JAXB how to handle the situation ?

Does anyone have any explanation on why glassfish v2.0 and v2.1 behaves so differently in this aspect ?

Thanks




 XML java.net RSS