-->
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: Lazy loding - how to achieve optional lazy loading
PostPosted: Fri Mar 19, 2004 2:12 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
It is possible to have lazy loading optional at runtime? I would like to have method on state less session bean (SLSB), where one parameter is: boolean lazyLoding

If I set lazyLoding=false, I would like to get all object graph from method of SLSB, when I set lazyLoading=true, I would like to retrieve only "first level of data", only object with its properties, not related classes, collections...

EXAMPLE of method I would like to implement:

In POJOs I have hibernate tag:
Code:
* @hibernate.class table="XXX_TBL" proxy="com.domainmodel.Xxx"

Code:
    public List findPOJOS(boolean lazyLoading, String sQuery) throws ExceptionDAO {
        List results = null;
        Session session = getSession();
        try {
            results = (List) session.find(sQuery);
            if (!lazyLoading) {
                // here I don't know how to write generic code
                // begin - this code is not ok
                for (int i = 0; i < results.size(); i++) {
                    Object o = (Object) results.get(i);
                    Hibernate.initialize(o);
                    // here should come recursion to initialize
                    // all objects in graph
                }
                // end - this code is not ok
            }
        } catch (HibernateException he) {
            he.printStackTrace();
            throw new ExceptionDAO(he);
        } finally {
            closeSession(session);
        }
        return results;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 7:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No, set relation as lazy and use the fetch keyword

_________________
Emmanuel


Top
 Profile  
 
 Post subject: ?
PostPosted: Mon Mar 22, 2004 5:24 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Sorry, I don't understand how to do that. I think it is impossible in my case.

I have entity - POJO object that contain many-to-one and one-to-many mappings.

I would like to decide in runtime, if objects from other tables than base are loaded from db.

Please look at my submission in next topic:
topicUrl -> http://forum.hibernate.org/viewtopic.php?t=928960
submission -> Posted: Tue Mar 16, 2004 10:28 am Post subject: solution

Code:
// loading without foreign entity
try {
  results = (List) session.find("from InstrumentHistory");
} catch (HibernateException he) {
     he.printStackTrace();
} finally {
     session.close();
}

/*
  here I would like to read fields of object
  but there is problem, because session is closed
  and some fields are "proxied", so they are not null,
  and f.e. serializel try to read from this fields,
  runtime error occures: HibernateException: Could not initialize proxy - the owning Session was closed

  I would like to have this fiels null (in case of lazyLoding=true).
  How can I achieve it.
*/


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.