diff -u -r -x '*.jar' -x '*.class' -x '*.kota' -x '*.html' -x target -x '*.orig' -x '*.zip' compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/DefaultHibernateQueryProvider.java compass-1.2/src/main/src/org/compass/gps/device/hibernate/DefaultHibernateQueryProvider.java
--- compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/DefaultHibernateQueryProvider.java	2007-07-30 00:59:52.000000000 +0200
+++ compass-1.2/src/main/src/org/compass/gps/device/hibernate/DefaultHibernateQueryProvider.java	2007-10-25 09:53:27.000000000 +0200
@@ -18,7 +18,10 @@
 
 import org.compass.gps.device.hibernate.entities.EntityInformation;
 import org.hibernate.Query;
+import org.hibernate.Criteria;
 import org.hibernate.Session;
+import org.hibernate.criterion.Order;
+import org.hibernate.metadata.ClassMetadata;
 
 /**
  * A simple Hibernate query provider based on a select statement. The select
@@ -62,7 +65,14 @@
         return session.createQuery("from " + entityInformation.getName());
     }
 
+    public Criteria createCriteria(Session session, EntityInformation entityInformation) {
+    	Class type = entityInformation.getEntityClass();
+    	ClassMetadata metadata = session.getSessionFactory().getClassMetadata(type);
+    	return session.createCriteria(type).
+    		addOrder(Order.asc(metadata.getIdentifierPropertyName()));
+    }
+
     public String toString() {
         return "QueryProvider[" + selectQuery + "]";
     }
-}
\ No newline at end of file
+}
diff -u -r -x '*.jar' -x '*.class' -x '*.kota' -x '*.html' -x target -x '*.orig' -x '*.zip' compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/HibernateQueryProvider.java compass-1.2/src/main/src/org/compass/gps/device/hibernate/HibernateQueryProvider.java
--- compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/HibernateQueryProvider.java	2007-07-29 21:59:18.000000000 +0200
+++ compass-1.2/src/main/src/org/compass/gps/device/hibernate/HibernateQueryProvider.java	2007-10-25 09:53:27.000000000 +0200
@@ -18,6 +18,7 @@
 
 import org.compass.gps.device.hibernate.entities.EntityInformation;
 import org.hibernate.Query;
+import org.hibernate.Criteria;
 import org.hibernate.Session;
 
 /**
@@ -37,4 +38,16 @@
      * @return the Hibernate query
      */
     Query createQuery(Session session, EntityInformation entityInformation);
-}
\ No newline at end of file
+
+    /**
+     * Create a Criteria query based on the <code>EntityManager</code>
+     * and the {@link org.compass.gps.device.hibernate.entities.EntityInformation}.
+     * Criteria queries respect customizations about eager fetching of 
+     * collections.
+     * @param session           The Hibernate session to create the query with
+     * @param entityInformation The enity information to create the query with
+     * @return the Hibernate query
+     *
+     */
+    Criteria createCriteria(Session session, EntityInformation entityInformation);
+}
diff -u -r -x '*.jar' -x '*.class' -x '*.kota' -x '*.html' -x target -x '*.orig' -x '*.zip' compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/indexer/ScrollableHibernateIndexEntitiesIndexer.java compass-1.2/src/main/src/org/compass/gps/device/hibernate/indexer/ScrollableHibernateIndexEntitiesIndexer.java
--- compass-1.2.orig/src/main/src/org/compass/gps/device/hibernate/indexer/ScrollableHibernateIndexEntitiesIndexer.java	2007-07-29 23:16:14.000000000 +0200
+++ compass-1.2/src/main/src/org/compass/gps/device/hibernate/indexer/ScrollableHibernateIndexEntitiesIndexer.java	2007-10-25 09:54:21.000000000 +0200
@@ -24,6 +24,7 @@
 import org.compass.gps.device.hibernate.entities.EntityInformation;
 import org.compass.gps.device.support.parallel.IndexEntity;
 import org.hibernate.Query;
+import org.hibernate.Criteria;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.Session;
@@ -64,15 +65,55 @@
                     log.debug(device.buildMessage("Indexing entities [" + entityInformation.getName() + "] using query ["
                             + entityInformation.getQueryProvider() + "]"));
                 }
-                Query query = entityInformation.getQueryProvider().createQuery(hibernateSession, entityInformation);
-                // TODO how can we set the fetchCount?
+                Criteria query = entityInformation.getQueryProvider().createCriteria(hibernateSession, entityInformation);
+                query.setFetchSize(device.getFetchCount());
                 cursor = query.scroll(ScrollMode.FORWARD_ONLY);
+                class RowBuffer {
+                	private Object[] buffer = new Object[device.getFetchCount()]; 
+                	private int index = 0;
+                	private CompassSession compassSession;
+                	private Session hibernateSession;
+                	
+                	RowBuffer(CompassSession compassSession, Session hibernateSession) {
+                		this.compassSession = compassSession;
+                		this.hibernateSession = hibernateSession;
+                	}
+                	public void put(Object row)
+                	{
+                		if (index >= buffer.length)
+                			flush();
+                		buffer[index] = row;
+                		index++;
+                	}
+                	public void close()
+                	{
+                		flush();
+                		buffer = null;
+                	}
+                	private void flush()
+                	{
+                		for (int i = 0; i < index; i++) {
+                			compassSession.create(buffer[i]);
+                		}
+                        compassSession.evictAll();
+                		hibernateSession.clear();
+                        index = 0;
+                	}
+                }
+                RowBuffer buffer = new RowBuffer(session, hibernateSession);
+                Object prev = null;
                 while (cursor.next()) {
                     Object item = cursor.get(0);
-                    session.create(item);
-                    hibernateSession.evict(item);
-                    session.evictAll();
+                    if (item != prev && prev != null) {
+                    	buffer.put(prev);
+                    }
+                    prev = item;
+
+                }
+                if (prev != null) {
+                	buffer.put(prev);
                 }
+                buffer.close();
                 cursor.close();
                 session.evictAll();
                 hibernateTransaction.commit();
@@ -101,4 +142,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}


