-->
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.  [ 2 posts ] 
Author Message
 Post subject: Turning off lazy loading at runtime
PostPosted: Wed Dec 22, 2004 4:46 pm 
Newbie

Joined: Thu Dec 09, 2004 10:44 am
Posts: 2
Location: Brasil
Hibernate version:
2.1.7
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi!
Im working in an application with a seperate business tier. In this case the business logic must load all collections that will be needed by the web tier before returning. This means that the business tier should load all the data and return it already initialized to the presentation/web tier that is required for a particular use case. I have read that one solution is to call Hibernate.initialize() for each collection that will be needed in the web tier before the session is closed. But using this approach I will have to iterate in the entire collection calling Hibernate.initialize on each row to load the mapping collections.
I tried to use another approach turning of lazy loading at runtime as follows:

//iterates in the collection of mapped collection that the web tier will need
for (int i = 0; i < filter.collectionsToInitializeCount(); i++) {
//instantiate a new configuration object
Configuration c = new Configuration().configure();
//get the collection mapping from the XML mapping file
net.sf.hibernate.mapping.Collection col = c.getCollectionMapping
(filter.getCollectionToInitialize(i).toString());
//changes the "lazy" loading attribute of the collection mapping to false
col.setLazy(false);
//open a new session based on the new mapping
session = c.buildSessionFactory().openSession();
}

But the lazy loading that is originally "true" keeps "true" and the collection is not initialized. Is this approach correct? If so what is wrong with the code?

_________________
Best regards


Top
 Profile  
 
 Post subject: use buildMappings
PostPosted: Fri Nov 14, 2008 9:14 am 
Newbie

Joined: Fri Nov 14, 2008 9:11 am
Posts: 1
I am using this one in Hibernate 3:

private void turnOffLazy(Configuration cfg) {
cfg.buildMappings();
Iterator<?> iter = cfg.getClassMappings();
while (iter.hasNext()) {
PersistentClass persistentClass = (PersistentClass) iter.next();
persistentClass.setLazy(false);
Iterator<?> iter2 = persistentClass.getPropertyIterator();
while (iter2.hasNext()) {
Property prop = (Property) iter2.next();
prop.setLazy(false);
Value val = prop.getValue();
if (val != null && val instanceof Fetchable) {
Fetchable f = (Fetchable) val;
f.setLazy(false);
}
}
}
iter = cfg.getCollectionMappings();
while (iter.hasNext()) {
Collection collection = (Collection) iter.next();
collection.setLazy(false);
}
}

But not sure if it's working...


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