-->
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.  [ 4 posts ] 
Author Message
 Post subject: Serialization, disable lazy loading
PostPosted: Tue Aug 14, 2007 4:02 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hibernate version:
3.0.5
Mapping documents:
Contains various classes with lazy collections
Code between sessionFactory.openSession() and session.close():
// Get list of Object X
// Serialize list
Name and version of the database you are using:
Oracle 9




Hello,

We have an hibernate configuration with various classes defined in various included hbm.xml files. Those work well in our webapp environment, using leazy loading as much as possible to reduce memory footprint per user session.

We are now trying to develop a small standalon application that
1) uses the same classes and mapping
2) uses the same database

This small application will have to serialize/deserialize a full hibernate object tree for backup purposes. Unfortunately, because lazy collections are not initialized, they don't make their way in the serialized object tree. They just keep their lazy state at unserialization.

For this purpose, we would like to have hibernate completly disable lazy initalization. Is there a configuration parameter we can use for that? What we would like is hibernate ignore all lazy="true" in hbm.xml files and have default lazy loading to false. That way, if we get Object X, we are pretty sure it comes with the full set of dependent datas.


If it's not possible, are there recommanded ways to make an object tree backup using hibernate? Sql scripts don't work because we have LOBs in our schema. And using the oracle backup tools to backup the complete schema is too much, we want to be able to be selective about restore.


If it's impossible, we will iterate over all collections, but it may be a lot of useless work if hibernate already provide helpful tools for this...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 5:39 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can override the lazy settings in the mapping file by using "join fetch" in hql or setting FetchMode.JOIN in criteria queries.
e.g. Imagine my classes are as follows:
Root->collection of Level1->collection of Level2

I can load my one root object with its lazy collections already initialised using this hql:
Root root = (Root)s.createQuery("from Root r join fetch r.level1s l1 join fetch l1.level2s").uniqueResult();

or criteria:
Root root = (Root)s.createCriteria(Root.class)
.setFetchMode("level1s", FetchMode.JOIN)
.setFetchMode("level1s.level2s", FetchMode.JOIN)
.uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 5:51 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hi,

thanks for suggestion. However, this get to the same problem as if i was to explore the collection manually before serializing

1) as soon as, at one level, i add a collection i have to add it to the query. It's nearly the same as adding a 2 lines java code, it quickly becomes unmaintanable (currently the root item has about 15 Collections, some of them contains a Collection too).
2) I have, in my shema root-> anotherRoot -> ... , there is no join query that can handle such transitive shema.


Other suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 7:44 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You could use reflection to find methods that return collections and iterate through the elements recursively:

CollectionNavigator.navigate(rootObject);

Code:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class CollectionNavigator {

   private Set<Collection<?>> visitedCollections = new HashSet<Collection<?>>();
   
   public static void navigate(Object object) {
      CollectionNavigator navigator = new CollectionNavigator();
      navigator.navigateObject(object);
   }
   
   public void navigateObject(Object object) {
      if (object == null) {
         return;
      }

      Method[] methods = object.getClass().getMethods();
      for (int i=0; i<methods.length; i++) {
         if (methodReturnsCollection(methods[i])) {
            try {
               Collection<?> collection = (Collection<?>)methods[i].invoke(object);
               navigateCollection(collection);
            } catch (IllegalArgumentException e) {
               throwRuntimeException(methods[i], e);
            } catch (IllegalAccessException e) {
               throwRuntimeException(methods[i], e);
            } catch (InvocationTargetException e) {
               throwRuntimeException(methods[i], e);
            }
         }
      }
   }
   
   private void navigateCollection(Collection<?> collection) {
      if (collection == null) {
         return;
      }

      if (visitedCollections.contains(collection)) {
         // avoid recursive references in collections
         return;
      }
      
      visitedCollections.add(collection);
      for (Object object : collection) {
         navigateObject(object);
      }
   }

   private boolean methodReturnsCollection(Method method) {
      Class<?> methodReturnType = method.getReturnType();
      Class<?>[] interfaces = methodReturnType.getInterfaces();
      for (int i=0; i<interfaces.length; i++) {
         if (interfaces[i].equals(Collection.class)) {
            return true;
         }
      }
      return false;
   }

   private void throwRuntimeException(Method method, Exception e) {
      throw new RuntimeException("CollectionNavigator failed to invoke method: "+method.getName(), e);
   }
}


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