-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Persistent Object to XML
PostPosted: Sat Sep 01, 2007 2:43 am 
Newbie

Joined: Sat Sep 01, 2007 1:32 am
Posts: 5
I am trying to generate XML of my persistent objects using XMLEncoder but facing many errors like "lazyInitializationException", "nullpointer" and "timeStampException".

I searched on this forum but could not find an concrete solution to this problem. It would be very difficult to turn off the lazy loading because there is a very deep and complex hirarchy in my domain objects. I have more than 200 tables and classes in my schema. I can choose the specific relationships using Join/Select/Subselect but the problem is the XML encoder cannot generate XML of the objects in collections and I find a empty set in XML.
I dont need the collections inside the objects inside the collections of my object. I mean I need XMLs only upto the 3 level of hierarchy like
{Object.Properties, Object.Object, Object.Collections{Objects}}

I have also tried the XStream api but I get the following exception.
Code:
com.thoughtworks.xstream.converters.ConversionException: Cannot handle CGLIB enhanced proxies with multiple callbacks

I found this is a problem with XStream and hibernate combination of newer version and won't be fixed. So I suppose XStream is not an Option for me.

I also tried to traverse the entire object using reflection and call all the getter methods and traverse all object of collections recursively but the problem persist and over and above this option is not good one from performance point of view.

Code:
private HashSet set = new HashSet();
   
    public void traverseObject(Object source)
    {
        if(set.contains(source))
        {
            return;
        }
        try{
            Class sourceType = source.getClass();
            Method sourceMethods[] = sourceType.getDeclaredMethods();
            for(int i=0; i<sourceMethods.length; i++)
            {
                Method currentMethod = sourceMethods[i];
                String currentMethodName = currentMethod.getName();
                if(currentMethodName.startsWith("get") && currentMethod.getParameterTypes().length == 0)
                {
                    Object obj = currentMethod.invoke(source, null);
                    if(obj != null && !(obj instanceof String) && !(obj instanceof Date) && !(obj instanceof Long) && !(obj instanceof Collection) )
                    {
                        if(obj instanceof CGLIBLazyInitializer)
                            continue;
                        else if(obj instanceof Collection)
                        {
                            Collection collection = (Collection)obj;
                            for (Object object : collection)
                            {
                                traverseObject(object);
                                set.add(object);
                             }
                        }
                        else
                        {
                            traverseObject(obj);                           
                        }
                        set.add(obj);
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }



Another option I read about was XMLDataBinder but I observe that its not available in Hibernate3.

I would prefer to have some solution for XMLEncoder first cause I have code to parse the xml generated using encoder. A change of schema will require the change of code for xml parsing.

Following is the information of my dev environment. Please Help. Thanks in advance....

Hibernate version:3.2.4 sp1

Spring version:2.0.6

Full stack trace of any exception that occurs:
java.lang.InstantiationException: java.sql.Timestamp
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: java.sql.Timestamp
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
Continuing ...


Name and version of the database you are using:MySql 5.0.22
[/code]


Top
 Profile  
 
 Post subject: Re: Persistent Object to XML
PostPosted: Sat Sep 01, 2007 3:07 am 
Beginner
Beginner

Joined: Tue Oct 10, 2006 3:23 am
Posts: 33
Hi there,
Funny I just finished a piece of my project that deals exactly with this. XStream works very well in fact. The API is easy to extend and you can, when you encounter an uninitialized collection, either ignore them or initialyze them.

Take a look at the MapperWrapper api.

It's a little tricky but there are posts in the Xstream forum that talk about it.

Basically your main problem is hib proxies and you need to replace them/fully initialize them before passing them to your xml engine.

The power of Xstream, I think, is that it lets you 'intercept' and modify the way the graph is descended.

Also I ended up up customizing the whole thing (with annotations) so that I could indicate where to stop serializing from the object graph as well as which field from an object should go in the object graph.

Send me your email addr, we can take this 'offline' as I think it belongs more in xstream (or whatever tool you use from producing xml) forum.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 01, 2007 3:16 am 
Newbie

Joined: Sat Sep 01, 2007 1:32 am
Posts: 5
Hi mooritze
Thanks for your such a prompt reply. Yes, proxies can be the problem. I will have a look at the XStream forum.



I will definitely get in touch with you and update you.

If anyone else has solved this problem some other way please let me know.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.