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]