-->
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.  [ 7 posts ] 
Author Message
 Post subject: Customisable serialization of lazy proxies.
PostPosted: Wed May 19, 2004 1:39 pm 
Beginner
Beginner

Joined: Fri Apr 02, 2004 1:20 pm
Posts: 23
Summary:

It would be nice if Hibernate supported three options for lazy proxy object serialization:

proxy - send the proxy (current behaviour)

initialize - initialize the proxy and send the target

nullify - uninitialized proxies get sent as null; if the proxy is initialised then the target is sent.


Background:

I am trying to open up a legacy system to heterogenous clients. We have wrapped the legacy system with a combination of EJBs and POJOs. Clients that want write-access to the legacy system use EJBs that persist using screen scraping. Practically all of the read-access is done using stateless session beans which return POJO's managed by Hibernate (i.e., the DAO pattern). We have put Apache Axis in front of the EJB container to make our lifes even more miserable.

At the moment we are trying to control the cut-points in the object graph returned by the stateless session beans. We need fairly fine grained control over whan objects are returned to the client for two reasons:

1) Populating the whole object graph from the top-level objects down would simply be too slow and

2) Apache Axis' BeanSerializer traverses the whole object graph returned by the stateless session bean. In doing so it hits a LazyInitialisationException because a) it is running in a different JVM, b) some of the components returned are lazy proxies and c) we have not reattached them. See the first point for the reason why we cannot automatically reattach them.

We have decided to cut the object graph in the session beans (i.e., leaf nodes are null, depending on the method called).

To prevent the LazyInitializationException in Axis we must explicitly nullify the individual properties that we don't want to return. For example:
Code:
   Contract contract = (Contract)query.uniqueResult()

   contract.setDeeplyNestedLazilyLoadedProperty(null);

   return contract;


However in some methods we /do/ want to return the lazily loaded properties. We cannot easily reattach the objects when they have reached the Apache Axis' VM so we call Hibernate.initialize() before returning.
Code:
   Contract contract = (Contract)query.uniqueResult()

   Hibernate.initialize(getDeeplyNestedLazilyLoadedProperty());

   return contract;


But -- we to remember to explicitly nullify or initialize any lazyily loaded properties of DeeplyNestedLazilyLoadedProperty.
Code:
   Contract contract = (Contract)query.uniqueResult()

   SomeProp prop = contract.getDeeplyNestedLazilyLoadedProperty();

   Hibernate.initialize(prop);

   prop.setPropertyA(null);
   
   Hibernate.initialize(prop.getPropertyB());

   prop.getPropertyB().setPropertyC(null);

   return contract;


Having three options for lazy proxy serialization would allow us to set some sensible defaults in our mapping files instead of having this kind of messy code repeated in our session beans.

What do you think?




    Top
     Profile  
     
     Post subject:
    PostPosted: Wed May 19, 2004 1:41 pm 
    Hibernate Team
    Hibernate Team

    Joined: Sun Sep 14, 2003 3:54 am
    Posts: 7256
    Location: Paris, France
    2. may lead to full DB loading
    3. will break the dettach/reattach feature

    _________________
    Emmanuel


    Top
     Profile  
     
     Post subject:
    PostPosted: Wed May 19, 2004 2:00 pm 
    CGLIB Developer
    CGLIB Developer

    Joined: Thu Aug 28, 2003 1:44 pm
    Posts: 1217
    Location: Vilnius, Lithuania
    The most simple solution must be to drop proxies and to use keys as properties. It looks evil, but it will work.


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu May 20, 2004 5:30 am 
    Beginner
    Beginner

    Joined: Fri Apr 02, 2004 1:20 pm
    Posts: 23
    emmanual

    each has its pros and cons -- for example 1) implies that:

    - the hibernate jars are in the clients classpath
    - the client is prepared to call Session.lock(detached, LockMode.NONE)
    - the client can make connections to the database (i.e., is not firewalled)

    In my case the client is basically a closed system -- I don't want to make it reattach lazy proxies, nor do I want it making connections to the database, nor do I want it getting LazyInitialisationException. So I have to make sure I never send it a lazy proxy -- this means I have to explicitly Hibernate.initialize() or null them out in my services tier. I can't just switch lazy proxies off altogether because of performance reasons.

    regarding your comments about 3) breaking the dettach/reattach feature -- well thats the point, I want to be able to disable it! :)


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu May 20, 2004 5:31 am 
    Beginner
    Beginner

    Joined: Fri Apr 02, 2004 1:20 pm
    Posts: 23
    baliukas -- can you elaborate?


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu May 20, 2004 11:42 am 
    CGLIB Developer
    CGLIB Developer

    Joined: Thu Aug 28, 2003 1:44 pm
    Posts: 1217
    Location: Vilnius, Lithuania
    niallsmart wrote:
    baliukas -- can you elaborate?

    One of ways if is to break object model and to move this problem to client.


    Top
     Profile  
     
     Post subject:
    PostPosted: Fri May 21, 2004 7:01 am 
    Beginner
    Beginner

    Joined: Fri Apr 02, 2004 1:20 pm
    Posts: 23
    Yeah, but as I've said twice already, I can't do that -- the client is just a stupid bean that serializes the object graph to XML.


    Top
     Profile  
     
    Display posts from previous:  Sort by  
    Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.