-->
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.  [ 11 posts ] 
Author Message
 Post subject: disable lazy loading via API?
PostPosted: Wed Jun 30, 2004 1:11 pm 
Newbie

Joined: Wed Jun 30, 2004 12:47 pm
Posts: 7
I have a set of objects which all use lazy loading. However, I have to ship data from a server to to a client with an identical database in large batches. I think about using JSX (java serialization to xml) fot that. Having all lazy="false" is great - just fetch the root object an serialize it and I can send the whole stuff with a single call. Tried it and it worked like a charm.

I would like to keep lazy="true" as a default and switch off all lazy loading for those transfer jobs, but did not find a way to do it. Did I overlook something, isn't it possible at all or will such a feature come in later releases?

Best Regards,
Johann


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Usually, you override fetching strategies with HQL and Criteria queries. If you like to disable/enable some setting globally, you might rebuild the SessionFactory after modifying the metadata from the Configuration programatically. This is easier than it sounds, get all PersistentClass objects from Configuration (the API), loop over all collections and setLazy(true). Then, build a new SessionFactory from this Configuration.

This is acceptable for once-a-day batch operations, I think.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 5:13 pm 
Newbie

Joined: Wed Jun 30, 2004 12:47 pm
Posts: 7
Thanks, that was what I was looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 3:07 pm 
Newbie

Joined: Wed Jul 07, 2004 12:52 pm
Posts: 9
For my case, this won't work.

I also need to serialize the graph to send to the client (who does not necessarily have hibernate).

I have lazy="true" set for all my classes. Since I have many, many associations, setting lazy="false" will result in a db error since the resulting query involves more than the Oracle limit of 1000 columns. I need to have the data fetched for the entire graph, but association-by-association, not all in one query.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 4:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, using ClassMetadata to walk the graph and call Hibernate.initialize() on everything should be a 50-liner probably.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 3:11 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Pardon, but what about eager fetching with join fetch and setFetchMode, there is thread where Christian explains that approach http://forum.hibernate.org/viewtopic.php?p=2208765.

If you use eager fetching you don't need to manipulate metadata in run-time for disabling lazy fetch.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 10:38 am 
Newbie

Joined: Wed Jul 07, 2004 12:52 pm
Posts: 9
I'm not quite sure how to use ClassMetaData to do this.
I can manually walk my object graph and call Hibernate.initialize() on everything, but I'd prefer a generic approach, so that this code doesn't have to change every time the object model does.

Can you point me to somewhere where this process is documented?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 6:35 pm 
Newbie

Joined: Wed Jul 07, 2004 12:52 pm
Posts: 9
More specifically, I'm not sure as to how to use the metadata graph to traverse the object graph.

I would like to write code that goes something like:

Code:
private void forceLoad(Object obj) throws HibernateException {
     Hibernate.initialize(obj);
     ClassMetadata md = sessionFactory().getClassMetadata(obj.getClass());
     for(int i=0; i < md.getPropertyTypes().length; i++) {
       Type t = md.getPropertyTypes()[i];
       if(t.isEntityType()) {
         Object assoc = //get the assoc. object
         forceLoad(assoc);
       } else if(t.isPersistentCollectionType()) {
         Collection coll = //get the assoc. collection
         Hibernate.initialize(coll);
          
         for(Iterator iter = coll.iterator; iter.hasNext();) {
              forceLoad(iter.next());
            }
       }
     }
  }


but as you can see I am not clear on how to use the metadata to tell me how to walk thew object graph.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 11:40 am 
Newbie

Joined: Wed Jul 07, 2004 12:52 pm
Posts: 9
There is a method on AbstractEntityPersister, getGetters() , which perhaps I could use in the above code, like:

Code:
...
if(t.isEntityType()) {
         Object assoc = ((AbstractEntityPersister)md).getGetters() [i].get(obj);
         forceLoad(assoc);
}
...



However, getGetters() is protected. Do I need to define my own persister in order to accomplish this?


Top
 Profile  
 
 Post subject: Re: disable lazy loading via API?
PostPosted: Thu May 28, 2009 4:30 am 
Newbie

Joined: Thu May 28, 2009 4:24 am
Posts: 1
I'm a bit change forceLoad() method, for me it's looks like this:
Code:
    protected SessionFactory getSessionFactory() {
        Session session = (Session) entityManager.getDelegate();
        return session.getSessionFactory();
    }

    public boolean isSkipClass(Class clazz) {
        Class[] skippedClasses = new Class[]{
              // classes to skip
        };
        return Arrays.asList(skippedClasses).contains(clazz);
    }

    protected void forceLoad(Object entity) throws HibernateException {
        if (entity == null) {
            return;
        }

        if (isSkipClass(entity.getClass())) {
            return;
        }

        ClassMetadata classMetadata = getSessionFactory().getClassMetadata(
                entity.getClass()
        );

        if (classMetadata == null) {
            return;
        }

        Hibernate.initialize(entity);

        for (int i = 0, n = classMetadata.getPropertyNames().length; i<n; i++) {
            String propertyName = classMetadata.getPropertyNames()[i];
            Type type = classMetadata.getPropertyType(propertyName);

            if (type.isEntityType()) {
                Object subEntity = classMetadata.getPropertyValue(entity, propertyName, EntityMode.POJO);
                forceLoad(subEntity);

            } if (type.isCollectionType()) {
                Collection collection = (Collection) classMetadata.getPropertyValue(entity, propertyName, EntityMode.POJO);
                if (collection != null && collection.size() > 0) {
                    for (Object collectionItem : collection) {
                        forceLoad(collectionItem);
                    }
                }
            }
        }
    }


Import section:

Code:
import com.thoughtworks.xstream.XStream;
import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.collection.PersistentBag;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.Type;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import javax.persistence.EntityManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;


Top
 Profile  
 
 Post subject: Re: disable lazy loading via API?
PostPosted: Tue Apr 24, 2012 6:02 am 
Newbie

Joined: Mon Apr 23, 2012 7:45 am
Posts: 1
hello Skywish,

it's very good thought to disable lazy loading.

but i have lazy="false" in hbm file and now I want enable lazy loading in programmatically without changing anything in hbm file.

Could you please suggest us and it could be better if you provide any example?

Thanks in advance!


regards,
shekhar reddy.


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