edu.northwestern.at.utils.db.hibernate
Class HibernateScrollIterator

java.lang.Object
  extended by edu.northwestern.at.utils.db.hibernate.HibernateScrollIterator
All Implemented Interfaces:
java.util.Iterator

public class HibernateScrollIterator
extends java.lang.Object
implements java.util.Iterator

Wraps a Hibernate ScrollableResults with a standard iterator.

Hibernate supported cursored result sets using the ScrollableResults interface. Cursored results sets do not require that the entire set of query results be retrieved to memory in one piece. In many cases, all one wants is to be able to iterate through the results in the usual fashion. This class wraps a Hibernate ScrollableResults with a standard Java Iterator to support simple sequential access.

Example:

PersistenceManager pm = new PersistenceManager(); ScrollableResults r = pm.scrollableQuery( ... );
HibernateScrollIterator iterator = new HibernateScrollIterator( r ); while ( iterator.hasNext() ) { Object o = iterator.next(); ... } iterator.close();

You may call the close() method explicitly as above to release the ScrollableResults object and its associated database cursor. These will also be released implicitly when the HibernateScrollIterator is finalized.


Field Summary
protected  boolean isClosed
          True if the ScrollableResults object has been closed.
protected  java.lang.Object nextRow
          The next database row to return.
protected  org.hibernate.ScrollableResults scrollableResults
          The Hibernate ScrollableResults object through which to iterate.
 
Constructor Summary
HibernateScrollIterator(org.hibernate.ScrollableResults scrollableResults)
          Wrap a Hibernate ScrollableResults with an iterator.
 
Method Summary
 void close()
          Close the Hibernate ScrollableResults field.
protected  void finalize()
          Handle object destruction.
 boolean hasNext()
          Iterator interface method: is there another database row available?
 boolean isClosed()
          Check if scrollable results closed.
 java.lang.Object next()
          Iterator interface method: return next row of data.
 void remove()
          Iterator interdace method; Remove current entry -- Not supported.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scrollableResults

protected org.hibernate.ScrollableResults scrollableResults
The Hibernate ScrollableResults object through which to iterate.


isClosed

protected boolean isClosed
True if the ScrollableResults object has been closed.


nextRow

protected java.lang.Object nextRow
The next database row to return.

Constructor Detail

HibernateScrollIterator

public HibernateScrollIterator(org.hibernate.ScrollableResults scrollableResults)
Wrap a Hibernate ScrollableResults with an iterator.

Parameters:
scrollableResults - The Hibernate scrollable results to wrap with an iterator.
Method Detail

hasNext

public boolean hasNext()
Iterator interface method: is there another database row available?

Specified by:
hasNext in interface java.util.Iterator
Returns:
true if another database row is available, false otherwise.

next

public java.lang.Object next()
Iterator interface method: return next row of data.

Specified by:
next in interface java.util.Iterator
Returns:
The next database row as an object.

remove

public void remove()
Iterator interdace method; Remove current entry -- Not supported.

Specified by:
remove in interface java.util.Iterator
Throws:
java.lang.UnsupportedOperationException - always

close

public void close()
Close the Hibernate ScrollableResults field.


isClosed

public boolean isClosed()
Check if scrollable results closed.


finalize

protected void finalize()
                 throws java.lang.Throwable
Handle object destruction.

Releases the Hibernate ScrollableResults before performing the usual finalization.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable