<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -12,10 +12,10 @@ import org.hivedb.HiveRuntimeException;
 import org.hivedb.annotations.AnnotationHelper;
 import org.hivedb.annotations.DataIndexDelegate;
 import org.hivedb.annotations.IndexType;
-import org.hivedb.configuration.entity.EntityIndexConfig;
-import org.hivedb.configuration.entity.EntityIndexConfigDelegator;
-import org.hivedb.configuration.entity.*;
-import org.hivedb.configuration.entity.EntityIndexConfigImpl;
+import org.hivedb.configuration.EntityConfig;
+import org.hivedb.configuration.EntityIndexConfig;
+import org.hivedb.configuration.EntityIndexConfigDelegator;
+import org.hivedb.configuration.EntityIndexConfigImpl;
 import org.hivedb.util.Lists;
 import org.hivedb.util.classgen.GenerateInstance;
 import org.hivedb.util.classgen.GeneratedClassFactory;
@@ -100,11 +100,11 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
       @SuppressWarnings(&quot;unchecked&quot;)
       public Collection&lt;Object&gt; execute(Session session) {
         Query query =
-          session.createQuery(
-            String.format(
-              &quot;select %s from %s&quot;,
-              propertyName,
-              GeneratedClassFactory.getGeneratedClass(config.getRepresentedInterface()).getSimpleName()));
+            session.createQuery(
+                String.format(
+                    &quot;select %s from %s&quot;,
+                    propertyName,
+                    GeneratedClassFactory.getGeneratedClass(config.getRepresentedInterface()).getSimpleName()));
         if (maxResults &gt; 0) {
           query.setFirstResult(firstResult);
           query.setMaxResults(maxResults);
@@ -132,15 +132,23 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
   }
 
   public Integer getCount(final String propertyName, final Object propertyValue) {
-    return (Integer) Atom.getFirstOrThrow(queryByProperties(propertyName, Collections.singletonMap(propertyName, propertyValue), 0, 0, true));
+    return (Integer) sumCounts(queryByProperties(propertyName, Collections.singletonMap(propertyName, propertyValue), 0, 0, true));
   }
 
   public Integer getCountByProperties(String partitioningPropertyName, Map&lt;String, Object&gt; propertyNameValueMap) {
-    return (Integer) Atom.getFirstOrThrow(queryByProperties(partitioningPropertyName, propertyNameValueMap, 0, 0, true));
+    return (Integer) sumCounts(queryByProperties(partitioningPropertyName, propertyNameValueMap, 0, 0, true));
   }
 
   public Integer getCountByProperties(String partitioningPropertyName, Map&lt;String, Object&gt; propertyNameValueMap, Integer firstResult, Integer maxResults) {
-    return (Integer) Atom.getFirstOrThrow(queryByProperties(partitioningPropertyName, propertyNameValueMap, firstResult, maxResults, true));
+    return (Integer) sumCounts(queryByProperties(partitioningPropertyName, propertyNameValueMap, firstResult, maxResults, true));
+  }
+
+  private Integer sumCounts(Collection&lt;Object&gt; objects) {
+    int count = 0;
+    for (Object object : objects) {
+      count += ((Number) object).intValue();
+    }
+    return count;
   }
 
   public Collection&lt;Object&gt; findByPropertyRange(final String propertyName, final Object minValue, final Object maxValue) {
@@ -154,8 +162,8 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
         @SuppressWarnings(&quot;unchecked&quot;)
         public Collection&lt;Object&gt; execute(Session session) {
           Query query = session.createQuery(String.format(&quot;from %s as x where x.%s between (:minValue, :maxValue)&quot;,
-            entityConfig.getRepresentedInterface().getSimpleName(),
-            indexConfig.getIndexName())
+              entityConfig.getRepresentedInterface().getSimpleName(),
+              indexConfig.getIndexName())
           ).setEntity(&quot;minValue&quot;, minValue).setEntity(&quot;maxValue&quot;, maxValue);
           return query.list();
         }
@@ -199,11 +207,11 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
         @SuppressWarnings(&quot;unchecked&quot;)
         public Collection&lt;Object&gt; execute(Session session) {
           Query query = session.createQuery(String.format(&quot;from %s as x where %s between (:minValue, :maxValue) order by x.%s asc limit %s, %s&quot;,
-            entityConfig.getRepresentedInterface().getSimpleName(),
-            indexConfig.getIndexName(),
-            entityConfig.getIdPropertyName(),
-            firstResult,
-            maxResults)
+              entityConfig.getRepresentedInterface().getSimpleName(),
+              indexConfig.getIndexName(),
+              entityConfig.getIdPropertyName(),
+              firstResult,
+              maxResults)
           ).setEntity(&quot;minValue&quot;, minValue).setEntity(&quot;maxValue&quot;, maxValue);
           return query.list();
         }
@@ -250,7 +258,7 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
 
   private boolean partionDimensionKeyHasChanged(Object entity) {
     return hive.directory().doesResourceIdExist(config.getResourceName(), config.getId(entity)) &amp;&amp;
-      !config.getPartitionKey(entity).equals(getHive().directory().getPrimaryIndexKeyOfResourceId(config.getResourceName(), config.getId(entity)));
+        !config.getPrimaryIndexKey(entity).equals(getHive().directory().getPrimaryIndexKeyOfResourceId(config.getResourceName(), config.getId(entity)));
   }
 
   public Collection&lt;Object&gt; saveAll(Collection&lt;Object&gt; collection) {
@@ -260,9 +268,9 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
     //o large save operations in manageable sized chunks
     for (int chunkId = 0; chunkId &lt; entities.size(); chunkId += BaseDataAccessObject.getSaveChunkSize()) {
       final Collection&lt;Object&gt; chunk =
-        entities.subList(
-          chunkId,
-          chunkId + getSaveChunkSize() &lt;= entities.size() ? chunkId + getSaveChunkSize() : entities.size());
+          entities.subList(
+              chunkId,
+              chunkId + getSaveChunkSize() &lt;= entities.size() ? chunkId + getSaveChunkSize() : entities.size());
 
       // If the partition dimension key has changed for any entity we assume that we need
       // to delete all entities before saving, in case the new partition dimension key
@@ -337,11 +345,11 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
     return session.get(getRespresentedClass(), id);
   }
 
-  private Collection&lt;Object&gt; queryByProperties(
-    String partitioningPropertyName,
-    final Map&lt;String, Object&gt; propertyNameValueMap,
-    final Integer firstResult, final Integer maxResults,
-    final boolean justCount) {
+  protected Collection&lt;Object&gt; queryByProperties(
+      String partitioningPropertyName,
+      final Map&lt;String, Object&gt; propertyNameValueMap,
+      final Integer firstResult, final Integer maxResults,
+      final boolean justCount) {
     final EntityIndexConfig entityIndexConfig = resolveEntityIndexConfig(partitioningPropertyName);
     Session session = createSessionForIndex(config, entityIndexConfig, propertyNameValueMap.get(partitioningPropertyName));
 
@@ -357,21 +365,21 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
       query = new QueryCallback() {
         public Collection&lt;Object&gt; execute(Session session) {
           Map&lt;String, Object&gt; revisedPropertyNameValueMap = Transform.toMap(
-            new Unary&lt;Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;, String&gt;() {
-              public String f(Map.Entry&lt;String, Map.Entry&lt;EntityIndexConfig, Object&gt;&gt; item) {
-                return item.getKey();
-              }
-            },
-            new Unary&lt;Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;, Object&gt;() {
-              public Object f(Map.Entry&lt;String, Map.Entry&lt;EntityIndexConfig, Object&gt;&gt; item) {
-                return item.getValue().getValue();
-              }
-            },
-            propertyNameEntityIndexConfigValueMap.entrySet());
+              new Unary&lt;Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;, String&gt;() {
+                public String f(Map.Entry&lt;String, Map.Entry&lt;EntityIndexConfig, Object&gt;&gt; item) {
+                  return item.getKey();
+                }
+              },
+              new Unary&lt;Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;, Object&gt;() {
+                public Object f(Map.Entry&lt;String, Map.Entry&lt;EntityIndexConfig, Object&gt;&gt; item) {
+                  return item.getValue().getValue();
+                }
+              },
+              propertyNameEntityIndexConfigValueMap.entrySet());
 
           return justCount
-            ? queryWithHQLRowCount(session, revisedPropertyNameValueMap, firstResult, maxResults)
-            : queryWithHQL(session, revisedPropertyNameValueMap, firstResult, maxResults);
+              ? queryWithHQLRowCount(session, revisedPropertyNameValueMap, firstResult, maxResults)
+              : queryWithHQL(session, revisedPropertyNameValueMap, firstResult, maxResults);
         }
       };
     else
@@ -394,21 +402,21 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
   }
 
   private Map&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt; createPropertyNameToValueMap(
-    final Map&lt;String, Object&gt; propertyNameValueMap) {
+      final Map&lt;String, Object&gt; propertyNameValueMap) {
     return
-      Transform.toOrderedMap(
-        new Unary&lt;String, Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;&gt;() {
-          public Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt; f(String propertyName) {
-            EntityIndexConfig entityIndexConfig = resolveEntityIndexConfig(propertyName);
-            DataIndexDelegate dataIndexDelegate = AnnotationHelper.getAnnotationDeeply(clazz, propertyName, DataIndexDelegate.class);
-            EntityIndexConfig resolvedEntityIndexConfig = (dataIndexDelegate != null)
-              ? resolveEntityIndexConfig(dataIndexDelegate.value())
-              : entityIndexConfig;
-            return new Pair&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;(
-              resolvedEntityIndexConfig.getPropertyName(),
-              new Pair&lt;EntityIndexConfig, Object&gt;(resolvedEntityIndexConfig, propertyNameValueMap.get(propertyName)));
-          }
-        }, propertyNameValueMap.keySet());
+        Transform.toOrderedMap(
+            new Unary&lt;String, Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;&gt;() {
+              public Entry&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt; f(String propertyName) {
+                EntityIndexConfig entityIndexConfig = resolveEntityIndexConfig(propertyName);
+                DataIndexDelegate dataIndexDelegate = AnnotationHelper.getAnnotationDeeply(clazz, propertyName, DataIndexDelegate.class);
+                EntityIndexConfig resolvedEntityIndexConfig = (dataIndexDelegate != null)
+                    ? resolveEntityIndexConfig(dataIndexDelegate.value())
+                    : entityIndexConfig;
+                return new Pair&lt;String, Entry&lt;EntityIndexConfig, Object&gt;&gt;(
+                    resolvedEntityIndexConfig.getPropertyName(),
+                    new Pair&lt;EntityIndexConfig, Object&gt;(resolvedEntityIndexConfig, propertyNameValueMap.get(propertyName)));
+              }
+            }, propertyNameValueMap.keySet());
   }
 
 
@@ -428,8 +436,8 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
   @SuppressWarnings(&quot;unchecked&quot;)
   protected Collection queryWithHQLRowCount(Session session, Map&lt;String, Object&gt; propertyNameValueMap, Integer firstResult, Integer maxResults) {
     String queryString = String.format(&quot;select count(%s) %s&quot;,
-      config.getIdPropertyName(),
-      createHQLQuery(propertyNameValueMap));
+        config.getIdPropertyName(),
+        createHQLQuery(propertyNameValueMap));
 
     Query query = session.createQuery(queryString);
     if (maxResults != 0) {
@@ -447,19 +455,19 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
 
   private String createHQLQuery(Map&lt;String, Object&gt; propertyNameValueMap) {
     return String.format(&quot;from %s as x where&quot;, GeneratedClassFactory.getGeneratedClass(config.getRepresentedInterface()).getSimpleName())
-      + Amass.join(
-      new Joiner&lt;Entry&lt;String, Object&gt;, String&gt;() {
-        @Override
-        public String f(Entry&lt;String, Object&gt; entry, String result) {
-          return result + &quot; and &quot; + toHql(entry);
-        }
-      },
-      new Unary&lt;Entry&lt;String, Object&gt;, String&gt;() {
-        public String f(Entry&lt;String, Object&gt; entry) {
-          return toHql(entry);
-        }
-      },
-      propertyNameValueMap.entrySet());
+        + Amass.join(
+        new Joiner&lt;Entry&lt;String, Object&gt;, String&gt;() {
+          @Override
+          public String f(Entry&lt;String, Object&gt; entry, String result) {
+            return result + &quot; and &quot; + toHql(entry);
+          }
+        },
+        new Unary&lt;Entry&lt;String, Object&gt;, String&gt;() {
+          public String f(Entry&lt;String, Object&gt; entry) {
+            return toHql(entry);
+          }
+        },
+        propertyNameValueMap.entrySet());
   }
 
   private String toHql(Entry&lt;String, Object&gt; entry) {
@@ -529,9 +537,9 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
           EntityIndexConfig entityIndexConfig = config.getEntityIndexConfig(delegatorPropertyName);
           String delegatePropertyName = AnnotationHelper.getAnnotationDeeply(clazz, delegatorPropertyName, DataIndexDelegate.class).value();
           GeneratedInstanceInterceptor.setProperty(
-            modified,
-            delegatePropertyName,
-            Filter.grepUnique(entityIndexConfig.getIndexValues(modified)));
+              modified,
+              delegatePropertyName,
+              Filter.grepUnique(entityIndexConfig.getIndexValues(modified)));
         }
         return modified;
       }
@@ -540,13 +548,13 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
 
   private boolean isPrimitiveCollection(final String propertyName) {
     return ReflectionTools.isCollectionProperty(config.getRepresentedInterface(), propertyName)
-      &amp;&amp; !ReflectionTools.isComplexCollectionItemProperty(config.getRepresentedInterface(), propertyName);
+        &amp;&amp; !ReflectionTools.isComplexCollectionItemProperty(config.getRepresentedInterface(), propertyName);
   }
 
   protected EntityIndexConfig resolveEntityIndexConfig(String propertyName) {
     return config.getPrimaryIndexKeyPropertyName().equals(propertyName)
-      ? createEntityIndexConfigForPartitionIndex(config)
-      : config.getEntityIndexConfig(propertyName);
+        ? createEntityIndexConfigForPartitionIndex(config)
+        : config.getEntityIndexConfig(propertyName);
   }
 
   private EntityIndexConfig createEntityIndexConfigForPartitionIndex(EntityConfig entityConfig) {
@@ -559,7 +567,7 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
     if (ReflectionTools.isCollectionProperty(config.getRepresentedInterface(), propertyName))
       if (ReflectionTools.isComplexCollectionItemProperty(config.getRepresentedInterface(), propertyName)) {
         criteria.createAlias(propertyName, &quot;x&quot;)
-          .add(Restrictions.eq(&quot;x.&quot; + indexConfig.getInnerClassPropertyName(), propertyValue));
+            .add(Restrictions.eq(&quot;x.&quot; + indexConfig.getInnerClassPropertyName(), propertyValue));
       } else
         throw new UnsupportedOperationException(&quot;This call should have used HQL, not Criteria&quot;);
     else
@@ -569,13 +577,13 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
   protected Session createSessionForIndex(EntityConfig entityConfig, EntityIndexConfig indexConfig, Object propertyValue) {
     if (indexConfig.getIndexType().equals(IndexType.Delegates))
       return factory.openSession(
-        ((EntityIndexConfigDelegator) indexConfig).getDelegateEntityConfig().getResourceName(),
-        propertyValue);
+          ((EntityIndexConfigDelegator) indexConfig).getDelegateEntityConfig().getResourceName(),
+          propertyValue);
     else if (indexConfig.getIndexType().equals(IndexType.Hive))
       return factory.openSession(
-        entityConfig.getResourceName(),
-        indexConfig.getIndexName(),
-        propertyValue);
+          entityConfig.getResourceName(),
+          indexConfig.getIndexName(),
+          propertyValue);
     else if (indexConfig.getIndexType().equals(IndexType.Data))
       return factory.openAllShardsSession();
     else if (indexConfig.getIndexType().equals(IndexType.Partition))
@@ -587,7 +595,7 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
     if (ReflectionTools.isCollectionProperty(config.getRepresentedInterface(), propertyName))
       if (ReflectionTools.isComplexCollectionItemProperty(config.getRepresentedInterface(), propertyName)) {
         criteria.createAlias(propertyName, &quot;x&quot;)
-          .add(Restrictions.between(&quot;x.&quot; + propertyName, minValue, maxValue));
+            .add(Restrictions.between(&quot;x.&quot; + propertyName, minValue, maxValue));
       } else
         throw new UnsupportedOperationException(&quot;This isn't working yet&quot;);
     else
@@ -599,15 +607,15 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
       doInTransaction(callback, getSession());
     } catch (org.hibernate.TransactionException dupe) {
       if (dupe.getCause().getClass().equals(org.hibernate.exception.ConstraintViolationException.class)
-        &amp;&amp; !exists(config.getId(entity))) {
-        doInTransaction(cleanupCallback, factory.openSession(config.getPartitionKey(entity)));
+          &amp;&amp; !exists(config.getId(entity))) {
+        doInTransaction(cleanupCallback, factory.openSession(config.getPrimaryIndexKey(entity)));
       } else {
         log.error(String.format(&quot;Detected an integrity constraint violation on the data node but %s with id %s exists in the directory.&quot;, config.getResourceName(), config.getId(entity)));
         throw dupe;
       }
     } catch (org.hibernate.exception.ConstraintViolationException dupe) {
       if (!exists(config.getId(entity))) {
-        doInTransaction(cleanupCallback, factory.openSession(config.getPartitionKey(entity)));
+        doInTransaction(cleanupCallback, factory.openSession(config.getPrimaryIndexKey(entity)));
       } else {
         log.error(String.format(&quot;Detected an integrity constraint violation on the data node but %s with id %s exists in the directory.&quot;, config.getResourceName(), config.getId(entity)));
         throw dupe;
@@ -620,14 +628,14 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
       doInTransaction(callback, getSession());
     } catch (org.hibernate.TransactionException dupe) {
       if (dupe.getCause().getClass().equals(org.hibernate.exception.ConstraintViolationException.class)
-        || dupe.getCause().getClass().equals(org.hibernate.StaleObjectStateException.class)) {
-        doInTransaction(cleanupCallback, factory.openSession(config.getPartitionKey(Atom.getFirstOrThrow(entities))));
+          || dupe.getCause().getClass().equals(org.hibernate.StaleObjectStateException.class)) {
+        doInTransaction(cleanupCallback, factory.openSession(config.getPrimaryIndexKey(Atom.getFirstOrThrow(entities))));
       } else {
         log.error(String.format(&quot;Detected an integrity constraint violation on the data node while doing a saveAll with entities of class %s.&quot;, config.getResourceName()));
         throw dupe;
       }
     } catch (org.hibernate.exception.ConstraintViolationException dupe) {
-      doInTransaction(cleanupCallback, factory.openSession(config.getPartitionKey(Atom.getFirstOrThrow(entities))));
+      doInTransaction(cleanupCallback, factory.openSession(config.getPrimaryIndexKey(Atom.getFirstOrThrow(entities))));
     }
   }
 
@@ -643,11 +651,11 @@ public class BaseDataAccessObject implements DataAccessObject&lt;Object, Serializab
   private void validateNonNull(final Collection&lt;Object&gt; collection) {
     if (Filter.isMatch(new Filter.NullPredicate&lt;Object&gt;(), collection)) {
       String ids = Amass.joinByToString(new Joiner.ConcatStrings&lt;String&gt;(&quot;, &quot;),
-        Transform.map(new Unary&lt;Object, String&gt;() {
-          public String f(Object item) {
-            return item != null ? config.getId(item).toString() : &quot;null&quot;;
-          }
-        }, collection));
+          Transform.map(new Unary&lt;Object, String&gt;() {
+            public String f(Object item) {
+              return item != null ? config.getId(item).toString() : &quot;null&quot;;
+            }
+          }, collection));
       throw new HiveRuntimeException(String.format(&quot;Encountered null items in collection: %s&quot;, ids));
     }
   }</diff>
      <filename>src/main/java/org/hivedb/hibernate/BaseDataAccessObject.java</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,6 @@
 package org.hivedb.hibernate;
 
+import org.hibernate.shards.strategy.access.SequentialShardAccessStrategy;
 import org.hivedb.Hive;
 import org.hivedb.HiveLockableException;
 import org.hivedb.util.classgen.GenerateInstance;
@@ -66,10 +67,10 @@ public class BaseDataAccessObjectTest extends HiveTest {
     assertEquals(report.hashCode(), found.hashCode());
     // Test find by multiple properties
     found = Atom.getFirstOrThrow(dao.findByProperties(&quot;regionCode&quot;, Transform.toMap(
-      new Entry[]{
-        new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, report.getRegionCode()),
-        new Pair&lt;String, Object&gt;(&quot;weatherEvents&quot;, Atom.getFirstOrThrow(report.getWeatherEvents()).getEventId()),
-      })));
+        new Entry[]{
+            new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, report.getRegionCode()),
+            new Pair&lt;String, Object&gt;(&quot;weatherEvents&quot;, Atom.getFirstOrThrow(report.getWeatherEvents()).getEventId()),
+        })));
     assertEquals(report.hashCode(), found.hashCode());
   }
 
@@ -79,15 +80,15 @@ public class BaseDataAccessObjectTest extends HiveTest {
     final DataAccessObject&lt;WeatherReport, Integer&gt; dao = (DataAccessObject&lt;WeatherReport, Integer&gt;) getDao(getGeneratedClass());
 
     Collection&lt;WeatherReport&gt; reports =
-      Generate.create(new Generator&lt;WeatherReport&gt;() {
-        public WeatherReport generate() {
-          WeatherReport report = new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
-          GeneratedInstanceInterceptor.setProperty(report, &quot;continent&quot;, &quot;Derkaderkastan&quot;);
-          GeneratedInstanceInterceptor.setProperty(report, &quot;temperature&quot;, 101);
-          GeneratedInstanceInterceptor.setProperty(report, &quot;sources&quot;, Arrays.asList(new Integer[]{101, 102, 103}));
-          return dao.save(report);
-        }
-      }, new NumberIterator(12));
+        Generate.create(new Generator&lt;WeatherReport&gt;() {
+          public WeatherReport generate() {
+            WeatherReport report = new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
+            GeneratedInstanceInterceptor.setProperty(report, &quot;continent&quot;, &quot;Derkaderkastan&quot;);
+            GeneratedInstanceInterceptor.setProperty(report, &quot;temperature&quot;, 101);
+            GeneratedInstanceInterceptor.setProperty(report, &quot;sources&quot;, Arrays.asList(new Integer[]{101, 102, 103}));
+            return dao.save(report);
+          }
+        }, new NumberIterator(12));
 
     for (final String property : new String[]{&quot;temperature&quot;, &quot;sources&quot;}) {
       Assert.assertEquals(dao.findByProperty(property, 101).size(), 12);
@@ -97,8 +98,8 @@ public class BaseDataAccessObjectTest extends HiveTest {
         }
       }, new NumberIterator(3))));
       Assert.assertEquals(
-        new HashSet&lt;WeatherReport&gt;(results).hashCode(),
-        new HashSet(reports).hashCode());
+          new HashSet&lt;WeatherReport&gt;(results).hashCode(),
+          new HashSet(reports).hashCode());
     }
   }
 
@@ -107,36 +108,36 @@ public class BaseDataAccessObjectTest extends HiveTest {
   public void testFindByPropertyRange() throws Exception {
 
     Collection&lt;WeatherReport&gt; reports =
-      Generate.create(new Generator&lt;WeatherReport&gt;() {
-        public WeatherReport generate() {
-          return new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
-        }
-      }, new NumberIterator(5));
+        Generate.create(new Generator&lt;WeatherReport&gt;() {
+          public WeatherReport generate() {
+            return new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
+          }
+        }, new NumberIterator(5));
 
     DataAccessObject&lt;WeatherReport, Integer&gt; dao = (DataAccessObject&lt;WeatherReport, Integer&gt;) getDao(getGeneratedClass());
     dao.saveAll(reports);
     //Collection&lt;WeatherReport&gt; x = dao.findByProperty(&quot;temperature&quot;, Atom.getFirst(reports).getTemperature());
     Integer min = Amass.min(
-      new Unary&lt;WeatherReport, Integer&gt;() {
-        public Integer f(WeatherReport weatherReport) {
-          return weatherReport.getTemperature();
-        }
-      },
-      reports,
-      Integer.class);
+        new Unary&lt;WeatherReport, Integer&gt;() {
+          public Integer f(WeatherReport weatherReport) {
+            return weatherReport.getTemperature();
+          }
+        },
+        reports,
+        Integer.class);
     Integer max = Amass.max(
-      new Unary&lt;WeatherReport, Integer&gt;() {
-        public Integer f(WeatherReport weatherReport) {
-          return weatherReport.getTemperature();
-        }
-      },
-      reports,
-      Integer.class);
+        new Unary&lt;WeatherReport, Integer&gt;() {
+          public Integer f(WeatherReport weatherReport) {
+            return weatherReport.getTemperature();
+          }
+        },
+        reports,
+        Integer.class);
     Collection&lt;WeatherReport&gt; range = dao.findByPropertyRange(&quot;temperature&quot;, min, max);
     assertEquals(reports.size(), range.size());
 
     Collection&lt;WeatherReport&gt; smallerRange =
-      dao.findByPropertyRange(&quot;temperature&quot;, Atom.getFirst(reports).getTemperature(), Atom.getFirst(reports).getTemperature());
+        dao.findByPropertyRange(&quot;temperature&quot;, Atom.getFirst(reports).getTemperature(), Atom.getFirst(reports).getTemperature());
     assertEquals(1, smallerRange.size());
   }
 
@@ -159,13 +160,13 @@ public class BaseDataAccessObjectTest extends HiveTest {
     final int finalMin = min;
     final int finalMax = max;
     Assert.assertEquals(
-      new HashSet&lt;WeatherReport&gt;(Transform.flatten(Transform.map(new Unary&lt;Integer, Collection&lt;WeatherReport&gt;&gt;() {
-        public Collection&lt;WeatherReport&gt; f(Integer i) {
-          final Collection&lt;WeatherReport&gt; value = dao.findByPropertyRange(&quot;temperature&quot;, finalMin, finalMax, (i - 1) * 4, 4);
-          return value;
-        }
-      }, new NumberIterator(3)))).hashCode(),
-      set.hashCode());
+        new HashSet&lt;WeatherReport&gt;(Transform.flatten(Transform.map(new Unary&lt;Integer, Collection&lt;WeatherReport&gt;&gt;() {
+          public Collection&lt;WeatherReport&gt; f(Integer i) {
+            final Collection&lt;WeatherReport&gt; value = dao.findByPropertyRange(&quot;temperature&quot;, finalMin, finalMax, (i - 1) * 4, 4);
+            return value;
+          }
+        }, new NumberIterator(3)))).hashCode(),
+        set.hashCode());
   }
 
   @SuppressWarnings(&quot;unchecked&quot;)
@@ -175,31 +176,31 @@ public class BaseDataAccessObjectTest extends HiveTest {
     final int temperature = random.nextInt();
     final List&lt;String&gt; partitionDimensionKeys = Arrays.asList(new String[]{&quot;Asia&quot;, &quot;Andromida&quot;});
     Collection&lt;WeatherReport&gt; reports =
-      Generate.create(new Generator&lt;WeatherReport&gt;() {
-        public WeatherReport generate() {
-          WeatherReport weatherReport = new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
-          // Set the same temperature for each partition dimension id. The partition dimension id will be calculated from the report id
-          GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;temperature&quot;, temperature + weatherReport.getReportId() % 2);
-          GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;continent&quot;, partitionDimensionKeys.get(weatherReport.getReportId() % 2));
-          GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;regionCode&quot;, 4);
-          return weatherReport;
-        }
-      }, new NumberIterator(5));
+        Generate.create(new Generator&lt;WeatherReport&gt;() {
+          public WeatherReport generate() {
+            WeatherReport weatherReport = new GenerateInstance&lt;WeatherReport&gt;(WeatherReport.class).generate();
+            // Set the same temperature for each partition dimension id. The partition dimension id will be calculated from the report id
+            GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;temperature&quot;, temperature + weatherReport.getReportId() % 2);
+            GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;continent&quot;, partitionDimensionKeys.get(weatherReport.getReportId() % 2));
+            GeneratedInstanceInterceptor.setProperty(weatherReport, &quot;regionCode&quot;, 4);
+            return weatherReport;
+          }
+        }, new NumberIterator(5));
 
     DataAccessObject&lt;WeatherReport, Integer&gt; dao = (DataAccessObject&lt;WeatherReport, Integer&gt;) getDao(getGeneratedClass());
     dao.saveAll(reports);
     Assert.assertEquals(dao.getCount(&quot;temperature&quot;, temperature) + dao.getCount(&quot;temperature&quot;, temperature + 1), 5);
     Assert.assertEquals(dao.getCountByRange(&quot;temperature&quot;, temperature, temperature + 1), (Integer) 5);
     Assert.assertEquals((Integer) (dao.getCountByProperties(&quot;temperature&quot;, Transform.toMap(
-      new Entry[]{
-        new Pair&lt;String, Object&gt;(&quot;temperature&quot;, temperature),
-        new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, 4),
-      })) +
-      dao.getCountByProperties(&quot;temperature&quot;, Transform.toMap(
         new Entry[]{
-          new Pair&lt;String, Object&gt;(&quot;temperature&quot;, temperature + 1),
-          new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, 4),
-        }))), (Integer) 5);
+            new Pair&lt;String, Object&gt;(&quot;temperature&quot;, temperature),
+            new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, 4),
+        })) +
+        dao.getCountByProperties(&quot;temperature&quot;, Transform.toMap(
+            new Entry[]{
+                new Pair&lt;String, Object&gt;(&quot;temperature&quot;, temperature + 1),
+                new Pair&lt;String, Object&gt;(&quot;regionCode&quot;, 4),
+            }))), (Integer) 5);
   }
 
   @SuppressWarnings(&quot;unchecked&quot;)
@@ -282,20 +283,20 @@ public class BaseDataAccessObjectTest extends HiveTest {
     // Make sure the resource is only on 1 hive node
     Assert.assertEquals(nodeIdsOfResourceId.size(), 1);
     // Make sure the node matches the new node
-    Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPartitionKey(newPrimaryIndexKey)));
+    Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(newPrimaryIndexKey)));
     // Make sure the entity can be fetched on the new node
     Assert.assertNotNull(dao.get(updated.getReportId()));
   }
 
   private String findPrimaryKeyOnDifferentNode(WeatherReport original)
-    throws HiveLockableException {
+      throws HiveLockableException {
     // Update the primary index key
     int i = 1;
     // Get a primary index key on a different node
     for (; i &lt; 100; i++) {
-      getHive().directory().insertPartitionKey(new Integer(i).toString());
-      if (!Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPartitionKey(new Integer(i).toString())).equals(
-        Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPartitionKey(original.getContinent()))))
+      getHive().directory().insertPrimaryIndexKey(new Integer(i).toString());
+      if (!Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(new Integer(i).toString())).equals(
+          Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(original.getContinent()))))
         break;
     }
     return new Integer(i).toString();
@@ -408,7 +409,7 @@ public class BaseDataAccessObjectTest extends HiveTest {
       // Make sure the resource is only on 1 hive node
       Assert.assertEquals(nodeIdsOfResourceId.size(), 1);
       // Make sure the node matches the new node
-      Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPartitionKey(newPrimaryIndexKey)));
+      Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(newPrimaryIndexKey)));
       // Make sure the entity can be fetched on the new node
       Assert.assertNotNull(dao.get(report.getReportId()));
     }
@@ -444,4 +445,60 @@ public class BaseDataAccessObjectTest extends HiveTest {
     // Make sure all instances are found across nodes
     Assert.assertEquals(INSTANCES, dao.findByProperty(&quot;latitude&quot;, 1d).size());
   }
+
+  /**
+   * Test illustrating bug 6520-6522.
+   * &lt;p/&gt;
+   * The count code issues a count query to each node. The broken code was then looking only at the count returned by the first node. This was
+   * only the correct behavior 1/n percent of the time. The correct behavior is to sum the counts from all the nodes.
+   */
+  @Test
+  public void shouldSumCountsFromAllNodes() throws Exception {
+    final QueryCountStubDAO dao = new QueryCountStubDAO(getGeneratedClass());
+
+    assertEquals(5, dao.getCount(&quot;foo&quot;, &quot;bar&quot;).intValue());
+    assertEquals(5, dao.getCount(&quot;foo&quot;, &quot;bar&quot;).intValue());
+    assertEquals(5, dao.getCount(&quot;foo&quot;, &quot;bar&quot;).intValue());
+
+    assertEquals(3, dao.callCount);
+  }
+
+  /**
+   * Overrides the counts returned in support of the shouldSumCountsFromAllNodes test.
+   */
+  public class QueryCountStubDAO extends BaseDataAccessObject {
+    int callCount = 0;
+
+    public QueryCountStubDAO(Class clazz) {
+      super(
+          config.getEntityConfig(clazz),
+          hive,
+          new HiveSessionFactoryBuilderImpl(
+              config,
+              getMappedClasses(),
+              hive,
+              new SequentialShardAccessStrategy()));
+    }
+
+    @Override
+    protected Collection&lt;Object&gt; queryByProperties(String partitioningPropertyName, Map&lt;String, Object&gt; propertyNameValueMap, Integer firstResult, Integer maxResults, boolean justCount) {
+      ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();
+      switch (callCount++) {
+        case 0:
+          list.add(0);
+          list.add(5);
+          return list;
+        case 1:
+          list.add(5);
+          list.add(0);
+          return list;
+        case 2:
+          list.add(2);
+          list.add(3);
+          return list;
+        default:
+          throw new RuntimeException(&quot;unexpect call&quot;);
+      }
+    }
+  }
 }</diff>
      <filename>src/test/java/org/hivedb/hibernate/BaseDataAccessObjectTest.java</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b93a2190d622713970729013625c5bf1f8655dff</id>
    </parent>
  </parents>
  <author>
    <name>Britt Crawford</name>
    <email>britt@illtemper.org</email>
  </author>
  <url>http://github.com/britt/hivedb/commit/559342c8bb226005555537127e604697fa6159e2</url>
  <id>559342c8bb226005555537127e604697fa6159e2</id>
  <committed-date>2009-01-19T11:37:12-08:00</committed-date>
  <authored-date>2009-01-19T11:37:12-08:00</authored-date>
  <message>Merged GetCountByProperties bug fix from master</message>
  <tree>bd69f8839ff9583ba554df79cca7ef53d8ec47c6</tree>
  <committer>
    <name>Britt Crawford</name>
    <email>britt@illtemper.org</email>
  </committer>
</commit>
