#821 - CQueryIteratorSimple not working with consecutive calls to hasNext() - ie. without matching calls to next()

This commit is contained in:
Robin Bygrave committed 2016-10-03 23:36:03 +13:00
1 parent 014a9aef6f
commit 4f98ca5d91
2 files changed
+45 -1

No files matched your search

@@ -83,6 +83,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
*/ */
private EntityBean currentBean; private EntityBean currentBean;
/**
* Caches hasNext for consecutive hasNext() calls.
*/
private boolean hasNextCache;
private final BeanPropertyAssocMany<?> lazyLoadManyProperty; private final BeanPropertyAssocMany<?> lazyLoadManyProperty;
private Object lazyLoadParentId; private Object lazyLoadParentId;
@@ -504,6 +509,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
if (auditFindIterate) { if (auditFindIterate) {
auditIterateNextBean(); auditIterateNextBean();
} }
hasNextCache = false;
return nextBean; return nextBean;
} }
@@ -513,7 +519,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
if (noMoreRows || cancelled || loadedBeanCount >= maxRowsLimit) { if (noMoreRows || cancelled || loadedBeanCount >= maxRowsLimit) {
return false; return false;
} }
return readNextBean(); if (hasNextCache) {
return true;
}
hasNextCache = readNextBean();
return hasNextCache;
} }
} }
@@ -46,6 +46,40 @@ public class TestQueryFindIterate extends BaseTestCase {
assertEquals(2, count.get()); assertEquals(2, count.get());
} }
@Test
public void test_hasNext_hasNext() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
QueryIterator<Customer> queryIterator = server.find(Customer.class)
.where()
.isNotNull("name")
.setMaxRows(3)
.order().asc("id")
.findIterate();
try {
// check that hasNext does not move to the next bean
assertTrue(queryIterator.hasNext());
assertTrue(queryIterator.hasNext());
assertTrue(queryIterator.hasNext());
assertTrue(queryIterator.hasNext());
assertTrue(queryIterator.hasNext());
Customer first = queryIterator.next();
logger.info("first: {}", queryIterator.next());
assertEquals(first.getId(), Integer.valueOf(1));
while (queryIterator.hasNext()) {
logger.info("next: {}", queryIterator.next());
}
} finally {
queryIterator.close();
}
}
@Test @Test
public void findEach() { public void findEach() {