[xstream-user] Managing a parent child relationship

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Managing a parent child relationship

Kannan Ekanath

Hi,
We are using hibernate for O/R mapping. Basically lets assume three level of parent child relationships.
Class A {
   String property1;
   Integer property2;
   Set getAllBForThisA();
}

Class B {
   String property3;
   Set getAllCForThisB();
}

Class C {
}

Point here is that the Set that is returned is a "hibernate proxy". Essence is i want to call "Hibernate.initiailise()" before marshalling these objects.
I tried something like this,
            XStream xstream = new XStream();
            ReflectionProvider provider = new JVM().bestReflectionProvider();
            ClassMapper mapper = xstream.getClassMapper();
            xstream.registerConverter(new ReflectionConverter(mapper, provider) {

                public void marshal(Object original, HierarchicalStreamWriter writer, MarshallingContext context) {
                    Hibernate.initialize(original);
                    super.marshal(original, writer, context);
                }
               
            }, -5); //-5 i gave so that it is between normal and low. normal priority is for integer, string etc.
            A someA = //getInstanceof A;
            System.out.println(xstream.toXML(someA));
This however causes the code to get stuck into an infinite loop and ends up throwing OutOfmemory error. Tried to debug but it is trying to resolve some instance of B and goes into some sort of infinite loop and i run out of ideas. Can someone help?
          
--
Kannan Ekanath
Reply | Threaded
Open this post in threaded view
|

RE: [xstream-user] Managing a parent child relationship

Jörg Schaible
Hi Kannan,

Kannan Ekanath wrote on Monday, November 21, 2005 8:43 AM:

> Hi,
> We are using hibernate for O/R mapping. Basically lets assume three
> level of parent child relationships.
> Class A {
>    String property1;
>    Integer property2;
>    Set getAllBForThisA();
> }
>
> Class B {
>    String property3;
>    Set getAllCForThisB();
> }
>
> Class C {
> }
>
> Point here is that the Set that is returned is a "hibernate proxy".
> Essence is i want to call "Hibernate.initiailise()" before
> marshalling these objects.  
> I tried something like this,
>             XStream xstream = new XStream();
>             ReflectionProvider provider = new
>             JVM().bestReflectionProvider(); ClassMapper mapper =
>             xstream.getClassMapper(); xstream.registerConverter(new
> ReflectionConverter(mapper, provider) {
>
>                 public void marshal(Object original,
>                     HierarchicalStreamWriter writer,
>                     MarshallingContext context) {
>                 Hibernate.initialize(original);
> super.marshal(original, writer, context); }
>
>             }, -5); //-5 i gave so that it is between normal and low.
>             normal priority is for integer, string etc. A someA =
>             //getInstanceof A;
> System.out.println(xstream.toXML(someA));
> This however causes the code to get stuck into an infinite loop and
> ends up throwing OutOfmemory error. Tried to debug but it is trying
> to resolve some instance of B and goes into some sort of infinite
> loop and i run out of ideas. Can someone help?  

we had already users that tried to combine XStream with Hibernate, but they could not come up with a general solution. You might have a look at http://jira.codehaus.org/browse/XSTR-226 and may be you find a way to deal with this.

- Jörg