FileDocCategorySizeDatePackage
IteratorImpl.javaAPI DocHibernate 3.2.54135Thu Jun 07 13:22:50 BST 2007org.hibernate.impl

IteratorImpl

public final class IteratorImpl extends Object implements org.hibernate.engine.HibernateIterator
An implementation of java.util.Iterator that is returned by iterate() query execution methods.
author
Gavin King

Fields Summary
private static final Log
log
private ResultSet
rs
private final org.hibernate.event.EventSource
session
private final org.hibernate.type.Type[]
types
private final boolean
single
private Object
currentResult
private boolean
hasNext
private final String[]
names
private PreparedStatement
ps
private Object
nextResult
private org.hibernate.hql.HolderInstantiator
holderInstantiator
Constructors Summary
public IteratorImpl(ResultSet rs, PreparedStatement ps, org.hibernate.event.EventSource sess, org.hibernate.type.Type[] types, String[] columnNames, org.hibernate.hql.HolderInstantiator holderInstantiator)


	 
	         
	         
	         
	         
	         
	         
	   

		this.rs=rs;
		this.ps=ps;
		this.session = sess;
		this.types = types;
		this.names = columnNames;
		this.holderInstantiator = holderInstantiator;

		single = types.length==1;

		postNext();
	
Methods Summary
public voidclose()

		if (ps!=null) {
			try {
				log.debug("closing iterator");
				nextResult = null;
				session.getBatcher().closeQueryStatement(ps, rs);
				ps = null;
				rs = null;
				hasNext = false;
			}
			catch (SQLException e) {
				log.info( "Unable to close iterator", e );
				throw JDBCExceptionHelper.convert(
				        session.getFactory().getSQLExceptionConverter(),
				        e,
				        "Unable to close iterator"
					);
			}
			finally {
				try {
					session.getPersistenceContext().getLoadContexts().cleanup( rs );
				}
				catch( Throwable ignore ) {
					// ignore this error for now
					log.trace( "exception trying to cleanup load context : " + ignore.getMessage() );
				}
			}
		}
	
public booleanhasNext()

		return hasNext;
	
public java.lang.Objectnext()

		if ( !hasNext ) throw new NoSuchElementException("No more results");
		try {
			currentResult = nextResult;
			postNext();
			log.debug("returning current results");
			return currentResult;
		}
		catch (SQLException sqle) {
			throw JDBCExceptionHelper.convert(
					session.getFactory().getSQLExceptionConverter(),
					sqle,
					"could not get next iterator result"
				);
		}
	
private voidpostNext()

		this.hasNext = rs.next();
		if (!hasNext) {
			log.debug("exhausted results");
			close();
		}
		else {
			log.debug("retrieving next results");
			boolean isHolder = holderInstantiator.isRequired();

			if ( single && !isHolder ) {
				nextResult = types[0].nullSafeGet( rs, names[0], session, null );
			}
			else {
				Object[] nextResults = new Object[types.length];
				for (int i=0; i<types.length; i++) {
					nextResults[i] = types[i].nullSafeGet( rs, names[i], session, null );
				}

				if (isHolder) {
					nextResult = holderInstantiator.instantiate(nextResults);
				}
				else {
					nextResult = nextResults;
				}
			}

		}
	
public voidremove()

		if (!single) {
			throw new UnsupportedOperationException("Not a single column hibernate query result set");
		}
		if (currentResult==null) {
			throw new IllegalStateException("Called Iterator.remove() before next()");
		}
		if ( !( types[0] instanceof EntityType ) ) {
			throw new UnsupportedOperationException("Not an entity");
		}
		
		session.delete( 
				( (EntityType) types[0] ).getAssociatedEntityName(), 
				currentResult,
				false,
		        null
			);