-->
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: Adding iterate() to Criteria
PostPosted: Tue Oct 28, 2008 5:30 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.3.1

Actually, criteria querys return results of the type ScrollableResults, while HQL querys return an standard java Iterator. The problem, is that it makes difficult to change from criteria to query without changing the interface, as data types returned are different. Adding Iterator support to Criteria, should be as easy as wrapping ScrollableResult in an Iterator implementation, and it would allow to return an Iterator with both HQL and Criteria.


Code:
public class ScrollableResultsIteratorImpl implements Iterator {

   public ScrollableResultsIteratorImpl(ScrollableResults scrollableResult) {
      this.scrollableResult = scrollableResult;
   }

   private ScrollableResults scrollableResult = null;

   public boolean hasNext() {
      return !scrollableResult.isLast();
   }

   public Object next() {
      scrollableResult.next();
      return scrollableResult.get();
   }

   @Override
   public void remove() {
      throw new UnsupportedOperationException("remove not supported");      
   }

}


And just add an iterate() method to CriteriaImpl

Code:
   public Iterator iterate() {
      return new ScrollableResultsIteratorImpl(scroll(ScrollMode.FORWARD_ONLY));
   }


Top
 Profile  
 
 Post subject: Re: Adding iterate() to Criteria
PostPosted: Thu Jun 25, 2009 5:09 pm 
Newbie

Joined: Thu Jun 25, 2009 5:02 pm
Posts: 1
The above implementation broke for me on empty result sets under Hibernate 3.3.

I re-implemented with an Iterator.hasNext() that peeked ahead by calling scrollableResult.next() and setting aside any result to reveal on subsequent calls to Iterator.next(). This got around the problem.


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.