-->
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: setFetchMode not working
PostPosted: Fri Apr 07, 2006 3:41 pm 
Beginner
Beginner

Joined: Sun Jun 06, 2004 10:13 am
Posts: 20
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.x

Mapping documents:
generated from xdoclet

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:
oracle 9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

i'm trying to dynamically change the fetchMode, ie.. overriding the default mapping files lazy="false" to be true for some queries.

using the criteria API, there seems to be no magic combination that will have any effect on the default settings in the mapping files.

i have a Parent object with a list of Child objects

using:
session = sessionFactory.openSession();
Criteria sessionCriteria = criteria.getExecutableCriteria(session);
sessionCriteria.setFetchMode("child",FetchMode.SELECT);
return sessionCriteria.list();

setting the fetchmode to ANY of the available options seems to have NO effect stopping the child list from loading.

i've tried setFetchMod("Parent.child",FetchMode.SELECT);

even setFetchMod("foobar",FetchMode.SELECT) has no effect or even an error message.

has anyone got this to work? please help!

g


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:14 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The problem is that list of children is being fully populated even though you don't want it to be? Most likely, that's caused by having a criterion referring to the child collection. If the query needs to load something to execute, then hibernate doesn't throw it away if you don't want it: it optimizes things so that the in-memory list stays in memory, even if you don't need it any more.

Also note the javadocs for FetchMode.SELECT, as I think you're using it to do the opposite of what it actually does:
javadocs wrote:
static FetchMode SELECT: Fetch eagerly, using a separate select.

If you want to force a lazy load, simply omit that line. Assuming that child isn't referred to anywhere else in the criteria, hibernate now knows that nothing cares about the child collection, so it will automatically be proxied. Your line
Code:
sessionCriteria.setFetchMode("child",FetchMode.SELECT);
forces hiberante to eagerly load the collection (simply by referring to the collection in the criteria), and further, to use separate select statements to do that eager loading.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 1:43 pm 
Newbie

Joined: Thu May 10, 2007 1:38 pm
Posts: 3
That's not true. There's a big typo in the JavaDoc for FetchMode.SELECT which reads 'Fetch eagerly, using a separate select. Equivalent to fetch="select".'

It should be 'Fetch lazily, ...' It doesn't fetch associations; you'll get a lazyInitializationError.


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.