From: Gavin K. <ga...@ap...> - 2002-09-23 03:19:48
|
Yeah, I guess we should improve that part of the documentation. However, in my defence, the documentation includes the following passage. I think "The iterator will load objects on demand" is fairly suggestive...... Quote: ===== If you expect your query to return a very large number of objects, but you don't expect to use them all, you will get better performance from the iterate() methods, which return a java.util.Iterator. The iterator will load objects on demand. Iterator iter = sess.iterate("from q in class eg.Qux"); while ( iter.hasNext() ) { Qux qux = (Qux) iter.next(); // something we couldnt express in the query if ( qux.calculateComplicatedAlgorithm() ) { // delete the current instance iter.remove(); // dont need to process the rest break; } } Unfortunately java.util.Iterator does not declare any exceptions, so any SQL or Hibernate exceptions that occur are wrapped in a LazyInitializationException (a subclass of RuntimeException). The iterate() method also performs better if you expect that many of the objects are already loaded and cached by the session. For small result sets where no data is already cached, find() is a better choice. ----- Original Message ----- From: "Anton van Straaten" <an...@ap...> To: "hibernate list" <hib...@li...> Sent: Monday, September 23, 2002 1:06 PM Subject: [Hibernate] iterator docs > Gavin, > > Not that this will necessarily forestall all newbie questions about it, but > I think the Javadoc for iterator() could be more explicit. It currently > says: > > "Execute a query and return the results in an iterator. Results are lazily > instantiated. Write the given value to "?" in the query string. iterate() > is usually a less efficient way to retrieve objects than find()." > > A user might be forgiven for assuming that "lazily instantiated" means that > objects are instantiated lazily, when requested, from a single resultset for > the entire query. The docs in "6.3 Querying the database" explain this > better. Perhaps the last sentence above could be replaced with something > like this: > > "The iterator returned by iterate() will issue a separate database query for > each object that is requested, except for objects that are already loaded > and cached in the session. In many situations, it is more efficient to use > find()." > > Adjust to reflect reality... :) > > Anton > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |