diff --git a/src/main/java/com/avaje/ebean/Query.java b/src/main/java/com/avaje/ebean/Query.java index 56c453ff5..d6c44413e 100644 --- a/src/main/java/com/avaje/ebean/Query.java +++ b/src/main/java/com/avaje/ebean/Query.java @@ -340,22 +340,32 @@ public interface Query extends Serializable { */ public Query setAutofetch(boolean autofetch); + /** + * Set the default lazy loading batch size to use. + *

+ * When lazy loading is invoked on beans loaded by this query then this sets the + * batch size used to load those beans. + * + * @param lazyLoadBatchSize the number of beans to lazy load in a single batch + */ + public Query setLazyLoadBatchSize(int lazyLoadBatchSize); + /** * Explicitly set a comma delimited list of the properties to fetch on the * 'main' entity bean (aka partial object). Note that '*' means all * properties. - * + * *

    * Query<Customer> query = Ebean.createQuery(Customer.class);
-   * 
+   *
    * // Only fetch the customer id, name and status.
    * // This is described as a "Partial Object"
    * query.select("name, status");
    * query.where("lower(name) like :custname").setParameter("custname", "rob%");
-   * 
+   *
    * List<Customer> customerList = query.findList();
    * 
- * + * * @param fetchProperties * the properties to fetch for this bean (* = all properties). */ diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadBeanBuffer.java b/src/main/java/com/avaje/ebeaninternal/api/LoadBeanBuffer.java index f33c2181b..862c6f61d 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadBeanBuffer.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadBeanBuffer.java @@ -11,6 +11,8 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; */ public interface LoadBeanBuffer { + public int getBatchSize(); + public List getBatch(); public BeanDescriptor getBeanDescriptor(); diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadBeanRequest.java b/src/main/java/com/avaje/ebeaninternal/api/LoadBeanRequest.java index 4d4d2a443..e3af79aa4 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadBeanRequest.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadBeanRequest.java @@ -59,4 +59,7 @@ public class LoadBeanRequest extends LoadRequest { return lazyLoadProperty; } + public int getBatchSize() { + return getLoadContext().getBatchSize(); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadContext.java b/src/main/java/com/avaje/ebeaninternal/api/LoadContext.java index 2af1aa939..7c460cf79 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadContext.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadContext.java @@ -11,16 +11,16 @@ import com.avaje.ebeaninternal.server.core.OrmQueryRequest; */ public interface LoadContext { - /** - * Return the minimum batch size when using QueryIterator with query joins. - */ - public int getSecondaryQueriesMinBatchSize(OrmQueryRequest parentRequest, int defaultQueryBatch); + /** + * Return the minimum batch size when using QueryIterator with query joins. + */ + public int getSecondaryQueriesMinBatchSize(OrmQueryRequest parentRequest, int defaultQueryBatch); /** * Execute any secondary (+query) queries if there are any defined. * @param parentRequest the originating query request */ - public void executeSecondaryQueries(OrmQueryRequest parentRequest, int defaultQueryBatch); + public void executeSecondaryQueries(OrmQueryRequest parentRequest); /** * Register any secondary queries (+query or +lazy) with their diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadManyBuffer.java b/src/main/java/com/avaje/ebeaninternal/api/LoadManyBuffer.java index 75f1b9112..761f1df58 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadManyBuffer.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadManyBuffer.java @@ -13,6 +13,8 @@ import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; */ public interface LoadManyBuffer { + public int getBatchSize(); + public List> getBatch(); public BeanPropertyAssocMany getBeanProperty(); diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadManyRequest.java b/src/main/java/com/avaje/ebeaninternal/api/LoadManyRequest.java index ff48c0d0d..70d83eff7 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadManyRequest.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadManyRequest.java @@ -18,12 +18,11 @@ public class LoadManyRequest extends LoadRequest { private final boolean loadCache; - public LoadManyRequest(LoadManyBuffer loadContext, int batchSize, boolean lazy,boolean onlyIds, boolean loadCache) { - this(loadContext, null, batchSize, lazy, onlyIds, loadCache); + public LoadManyRequest(LoadManyBuffer loadContext, boolean lazy, boolean onlyIds, boolean loadCache) { + this(loadContext, null, lazy, onlyIds, loadCache); } - public LoadManyRequest(LoadManyBuffer loadContext, OrmQueryRequest parentRequest, int batchSize, boolean lazy, - boolean onlyIds, boolean loadCache) { + public LoadManyRequest(LoadManyBuffer loadContext, OrmQueryRequest parentRequest, boolean lazy, boolean onlyIds, boolean loadCache) { super(parentRequest, lazy); this.loadContext = loadContext; @@ -69,4 +68,10 @@ public class LoadManyRequest extends LoadRequest { return loadCache; } + /** + * Return the batch size used for this load context. + */ + public int getBatchSize() { + return loadContext.getBatchSize(); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/api/LoadSecondaryQuery.java b/src/main/java/com/avaje/ebeaninternal/api/LoadSecondaryQuery.java index 572746e57..23a90dcc5 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/LoadSecondaryQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/api/LoadSecondaryQuery.java @@ -13,9 +13,6 @@ public interface LoadSecondaryQuery { /** * Execute the secondary query with a given batch size. - * - * @param parentRequest - * the originating query request */ - public void loadSecondaryQuery(OrmQueryRequest parentRequest, int requestedBatchSize, boolean all); + public void loadSecondaryQuery(OrmQueryRequest parentRequest); } diff --git a/src/main/java/com/avaje/ebeaninternal/api/SpiQuery.java b/src/main/java/com/avaje/ebeaninternal/api/SpiQuery.java index 490145c5b..a998d4956 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/SpiQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/api/SpiQuery.java @@ -27,7 +27,7 @@ import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties; */ public interface SpiQuery extends Query { - public enum Mode { + public enum Mode { NORMAL(false), LAZYLOAD_MANY(false), LAZYLOAD_BEAN(true), REFRESH_BEAN(true); Mode(boolean loadContextBean) { this.loadContextBean = loadContextBean; @@ -89,6 +89,11 @@ public interface SpiQuery extends Query { */ public PersistenceContextScope getPersistenceContextScope(); + /** + * Return the default lazy load batch size. + */ + public int getLazyLoadBatchSize(); + /** * Return true if select all properties was used to ensure the property * invoking a lazy load was included in the query. diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultBeanLoader.java b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultBeanLoader.java index 4c602c7d1..8e2cfc385 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultBeanLoader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultBeanLoader.java @@ -130,6 +130,11 @@ public class DefaultBeanLoader { String mode = loadRequest.isLazy() ? "+lazy" : "+query"; query.setLoadDescription(mode, loadRequest.getDescription()); + if (loadRequest.isLazy()) { + // cascade the batch size (if set) for further lazy loading + query.setLazyLoadBatchSize(loadRequest.getBatchSize()); + } + // potentially changes the joins and selected properties ctx.configureQuery(query); @@ -198,7 +203,7 @@ public class DefaultBeanLoader { boolean useManyIdCache = beanCollection != null && parentDesc.isManyPropCaching(); if (useManyIdCache) { Boolean readOnly = null; - if (ebi != null && ebi.isReadOnly()) { + if (ebi.isReadOnly()) { readOnly = Boolean.TRUE; } if (parentDesc.cacheManyPropLoad(many, beanCollection, parentId, readOnly)) { @@ -240,10 +245,8 @@ public class DefaultBeanLoader { query.setLazyLoadManyPath(many.getName()); query.setPersistenceContext(pc); - if (ebi != null) { - if (ebi.isReadOnly()) { - query.setReadOnly(true); - } + if (ebi.isReadOnly()) { + query.setReadOnly(true); } server.findUnique(query, t); @@ -314,6 +317,11 @@ public class DefaultBeanLoader { String mode = loadRequest.isLazy() ? "+lazy" : "+query"; query.setLoadDescription(mode, loadRequest.getDescription()); + if (loadRequest.isLazy()) { + // cascade the batch size (if set) for further lazy loading + query.setLazyLoadBatchSize(loadRequest.getBatchSize()); + } + ctx.configureQuery(query, loadRequest.getLazyLoadProperty()); // make sure the query doesn't use the cache @@ -353,7 +361,6 @@ public class DefaultBeanLoader { private void refreshBeanInternal(EntityBean bean, SpiQuery.Mode mode, int embeddedOwnerIndex) { EntityBeanIntercept ebi = bean._ebean_getIntercept(); - ; PersistenceContext pc = ebi.getPersistenceContext(); if (Mode.REFRESH_BEAN == mode) { // need a new PersistenceContext for REFRESH @@ -375,9 +382,7 @@ public class DefaultBeanLoader { // a reference with no existing persistenceContext pc = new DefaultPersistenceContext(); pc.put(id, bean); - if (ebi != null) { - ebi.setPersistenceContext(pc); - } + ebi.setPersistenceContext(pc); } if (embeddedOwnerIndex == -1) { @@ -393,9 +398,7 @@ public class DefaultBeanLoader { } SpiQuery query = (SpiQuery) server.createQuery(desc.getBeanType()); - if (ebi != null) { - query.setLazyLoadProperty(ebi.getLazyLoadProperty()); - } + query.setLazyLoadProperty(ebi.getLazyLoadProperty()); if (embeddedOwnerIndex > -1) { String embeddedBeanPropertyName = ebi.getProperty(embeddedOwnerIndex); diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/OrmQueryRequest.java b/src/main/java/com/avaje/ebeaninternal/server/core/OrmQueryRequest.java index e57640695..54aa73169 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/OrmQueryRequest.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/OrmQueryRequest.java @@ -28,7 +28,6 @@ import com.avaje.ebeaninternal.server.loadcontext.DLoadContext; import com.avaje.ebeaninternal.server.query.CQueryPlan; import com.avaje.ebeaninternal.server.query.CancelableQuery; import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext; -import com.avaje.ebeaninternal.server.transaction.NoopPersistenceContext; /** * Wraps the objects involved in executing a Query. @@ -79,8 +78,8 @@ public final class OrmQueryRequest extends BeanRequest implements BeanQueryRe return ebeanServer.getDatabasePlatform().getLikeClause(); } - public void executeSecondaryQueries(int defaultQueryBatch) { - loadContext.executeSecondaryQueries(this, defaultQueryBatch); + public void executeSecondaryQueries() { + loadContext.executeSecondaryQueries(this); } /** @@ -405,4 +404,12 @@ public final class OrmQueryRequest extends BeanRequest implements BeanQueryRe return query.isLogSecondaryQuery(); } + /** + * Return the batch size for lazy loading on this bean query request. + */ + public int getLazyLoadBatchSize() { + + int batchSize = query.getLazyLoadBatchSize(); + return (batchSize > 0) ? batchSize : ebeanServer.getLazyLoadBatchSize(); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadBeanContext.java b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadBeanContext.java index ec7420213..b6e4bc3bc 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadBeanContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadBeanContext.java @@ -69,7 +69,7 @@ public class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContex return buffer; } - public void loadSecondaryQuery(OrmQueryRequest parentRequest, int requestedBatchSize, boolean all) { + public void loadSecondaryQuery(OrmQueryRequest parentRequest) { if (!queryFetch) { throw new IllegalStateException("Not expecting loadSecondaryQuery() to be called?"); @@ -111,7 +111,11 @@ public class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContex this.batchSize = batchSize; this.list = new ArrayList(batchSize); } - + + public int getBatchSize() { + return batchSize; + } + /** * Return true if the buffer is full. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadContext.java b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadContext.java index 7ae85b1c7..7a2976ed0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadContext.java @@ -56,7 +56,7 @@ public class DLoadContext implements LoadContext { this.persistenceContext = request.getPersistenceContext(); this.ebeanServer = request.getServer(); - this.defaultBatchSize = ebeanServer.getLazyLoadBatchSize(); + this.defaultBatchSize = request.getLazyLoadBatchSize(); this.rootDescriptor = request.getBeanDescriptor(); SpiQuery query = request.getQuery(); @@ -104,18 +104,13 @@ public class DLoadContext implements LoadContext { /** * Execute all the secondary queries. */ - public void executeSecondaryQueries(OrmQueryRequest parentRequest, int defaultQueryBatch) { + public void executeSecondaryQueries(OrmQueryRequest parentRequest) { if (secQuery != null){ for (int i = 0; i < secQuery.size(); i++) { OrmQueryProperties properties = secQuery.get(i); - - int batchSize = properties.getQueryFetchBatch(); - if (batchSize == 0){ - batchSize = defaultQueryBatch; - } LoadSecondaryQuery load = getLoadSecondaryQuery(properties.getPath()); - load.loadSecondaryQuery(parentRequest, batchSize, properties.isQueryFetchAll()); + load.loadSecondaryQuery(parentRequest); } } } @@ -201,10 +196,6 @@ public class DLoadContext implements LoadContext { return useAutofetchManager; } - public String getRelativePath() { - return relativePath; - } - protected String getFullPath(String path) { if (relativePath == null) { return path; diff --git a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadManyContext.java b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadManyContext.java index 18f64664a..4b5764f43 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadManyContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/loadcontext/DLoadManyContext.java @@ -83,8 +83,7 @@ public class DLoadManyContext extends DLoadBaseContext implements LoadManyContex bc.setLoader(0, currentBuffer); } - - public void loadSecondaryQuery(OrmQueryRequest parentRequest, int requestedBatchSize, boolean all){ + public void loadSecondaryQuery(OrmQueryRequest parentRequest) { if (!queryFetch) { throw new IllegalStateException("Not expecting loadSecondaryQuery() to be called?"); @@ -93,7 +92,7 @@ public class DLoadManyContext extends DLoadBaseContext implements LoadManyContex if (bufferList != null) { for (LoadBuffer loadBuffer : bufferList) { if (!loadBuffer.list.isEmpty()) { - LoadManyRequest req = new LoadManyRequest(loadBuffer, parentRequest, requestedBatchSize, false, false, false); + LoadManyRequest req = new LoadManyRequest(loadBuffer, parentRequest, false, false, false); parent.getEbeanServer().loadMany(req); if (!queryProps.isQueryFetchAll()) { // Stop - only fetch the first batch ... the rest will be lazy loaded @@ -127,6 +126,10 @@ public class DLoadManyContext extends DLoadBaseContext implements LoadManyContex this.batchSize = batchSize; this.list = new ArrayList>(batchSize); } + + public int getBatchSize() { + return batchSize; + } /** * Return true if the buffer is full. @@ -188,7 +191,7 @@ public class DLoadManyContext extends DLoadBaseContext implements LoadManyContex boolean useCache = context.hitCache && !onlyIds; if (useCache) { EntityBean ownerBean = bc.getOwnerBean(); - BeanDescriptor parentDesc = context.desc.getBeanDescriptor(ownerBean.getClass()); + BeanDescriptor parentDesc = context.desc.getBeanDescriptor(ownerBean.getClass()); Object parentId = parentDesc.getId(ownerBean); if (parentDesc.cacheManyPropLoad(context.property, bc, parentId, context.parent.isReadOnly())) { // we loaded the bean from cache @@ -199,7 +202,7 @@ public class DLoadManyContext extends DLoadBaseContext implements LoadManyContex // Should reduce the list by checking each beanCollection in the L2 first before executing the query - LoadManyRequest req = new LoadManyRequest(this, batchSize, true, onlyIds, useCache); + LoadManyRequest req = new LoadManyRequest(this, true, onlyIds, useCache); context.parent.getEbeanServer().loadMany(req); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java index 36526b3ed..c4a829785 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java @@ -25,14 +25,14 @@ public class CQueryEngine { private static final Logger logger = LoggerFactory.getLogger(CQueryEngine.class); - private final DatabasePlatform dbPlatform; - + private static final int defaultSecondaryQueryBatchSize = 100; + + private final boolean forwardOnlyHintOnFindIterate; + private final CQueryBuilder queryBuilder; - private final int defaultSecondaryQueryBatchSize = 100; - public CQueryEngine(DatabasePlatform dbPlatform, Binder binder) { - this.dbPlatform = dbPlatform; + this.forwardOnlyHintOnFindIterate = dbPlatform.isForwardOnlyHintOnFindIterate(); this.queryBuilder = new CQueryBuilder(dbPlatform, binder); } @@ -121,7 +121,7 @@ public class CQueryEngine { try { - if (!cquery.prepareBindExecuteQueryForwardOnly(dbPlatform.isForwardOnlyHintOnFindIterate())) { + if (!cquery.prepareBindExecuteQueryForwardOnly(forwardOnlyHintOnFindIterate)) { // query has been cancelled already logger.trace("Future fetch already cancelled"); return null; @@ -131,7 +131,15 @@ public class CQueryEngine { logSql(cquery); } + // first check batch sizes set on query joins int iterateBufferSize = request.getSecondaryQueriesMinBatchSize(defaultSecondaryQueryBatchSize); + if (iterateBufferSize < 1) { + // not set on query joins so check if batch size set on query itself + int queryBatch = request.getQuery().getLazyLoadBatchSize(); + if (queryBatch > 0) { + iterateBufferSize = queryBatch; + } + } QueryIterator readIterate = cquery.readIterate(iterateBufferSize, request); @@ -178,7 +186,7 @@ public class CQueryEngine { logFindManySummary(cquery); } - request.executeSecondaryQueries(defaultSecondaryQueryBatchSize); + request.executeSecondaryQueries(); return beanCollection; @@ -223,7 +231,7 @@ public class CQueryEngine { logFindBeanSummary(cquery); } - request.executeSecondaryQueries(defaultSecondaryQueryBatchSize); + request.executeSecondaryQueries(); return (T)bean; diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java index cc45d3237..ac7ebc3a6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java @@ -43,7 +43,7 @@ class CQueryIteratorWithBuffer implements QueryIterator { } } // execute secondary queries - request.executeSecondaryQueries(bufferSize); + request.executeSecondaryQueries(); } return !buffer.isEmpty(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java b/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java index 0f999fe64..f3fcaf2bc 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java @@ -88,7 +88,10 @@ public class DefaultOrmQuery implements SpiQuery { private int firstRow; - private int totalHits; + /** + * Lazy loading batch size (can override server wide default). + */ + private int lazyLoadBatchSize; /** * The where clause from a parsed query string. @@ -251,14 +254,6 @@ public class DefaultOrmQuery implements SpiQuery { } } - public int getTotalHits() { - return totalHits; - } - - public void setTotalHits(int totalHits) { - this.totalHits = totalHits; - } - @Override public Query apply(PathProperties pathProperties) { pathProperties.apply(this); @@ -295,6 +290,16 @@ public class DefaultOrmQuery implements SpiQuery { return this; } + @Override + public int getLazyLoadBatchSize() { + return lazyLoadBatchSize; + } + + public Query setLazyLoadBatchSize(int lazyLoadBatchSize) { + this.lazyLoadBatchSize = lazyLoadBatchSize; + return this; + } + public String getLazyLoadProperty() { return lazyLoadProperty; } @@ -1168,18 +1173,6 @@ public class DefaultOrmQuery implements SpiQuery { return query; } - public DefaultOrmQuery addWhere(String addToWhereClause) { - return where(addToWhereClause); - } - - public DefaultOrmQuery addWhere(Expression expression) { - return where(expression); - } - - public ExpressionList addWhere() { - return where(); - } - public DefaultOrmQuery where(String addToWhereClause) { if (additionalWhere == null) { additionalWhere = addToWhereClause; @@ -1217,18 +1210,6 @@ public class DefaultOrmQuery implements SpiQuery { } } - public DefaultOrmQuery addHaving(String addToHavingClause) { - return having(addToHavingClause); - } - - public DefaultOrmQuery addHaving(Expression expression) { - return having(expression); - } - - public ExpressionList addHaving() { - return having(); - } - public DefaultOrmQuery having(String addToHavingClause) { if (additionalHaving == null) { additionalHaving = addToHavingClause; diff --git a/src/test/java/com/avaje/tests/query/TestLimitQuery.java b/src/test/java/com/avaje/tests/query/TestLimitQuery.java index 251c9d836..faa42f580 100644 --- a/src/test/java/com/avaje/tests/query/TestLimitQuery.java +++ b/src/test/java/com/avaje/tests/query/TestLimitQuery.java @@ -1,35 +1,30 @@ package com.avaje.tests.query; -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; - import com.avaje.ebean.BaseTestCase; import com.avaje.ebean.Ebean; -import com.avaje.ebean.Expr; -import com.avaje.ebean.Junction; -import com.avaje.ebean.Query; import com.avaje.tests.model.basic.Customer; import com.avaje.tests.model.basic.ResetBasicData; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; public class TestLimitQuery extends BaseTestCase { @Test public void testHasManyWithLimit() { + ResetBasicData.reset(); - Query query = Ebean.find(Customer.class); - query.setAutofetch(false); - query.setFirstRow(0); - query.setMaxRows(10); + List customers = Ebean.find(Customer.class) + .setAutofetch(false) + .setFirstRow(0) + .setMaxRows(10) + .where().like("name", "%A%") + .findList(); - Junction junc = Expr.disjunction(query); - junc.add(Expr.like("name", "%A%")); - query.where(junc); + // should at least find the "Cust NoAddress" customer + Assert.assertTrue(customers.size() > 0); - List customer = query.findList(); - Assert.assertTrue(customer.size() > 0); // should at least find the - // "Cust NoAddress" customer } } \ No newline at end of file diff --git a/src/test/java/com/avaje/tests/query/lazy/TestQueryDefaultBatchSize.java b/src/test/java/com/avaje/tests/query/lazy/TestQueryDefaultBatchSize.java new file mode 100644 index 000000000..22beb68cd --- /dev/null +++ b/src/test/java/com/avaje/tests/query/lazy/TestQueryDefaultBatchSize.java @@ -0,0 +1,111 @@ +package com.avaje.tests.query.lazy; + +import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; +import com.avaje.ebean.QueryEachConsumer; +import com.avaje.ebean.config.ServerConfig; +import com.avaje.tests.model.basic.*; +import org.junit.Test; + +import java.util.List; + +public class TestQueryDefaultBatchSize extends BaseTestCase { + + @Test + public void test_findEach() { + + ResetBasicData.reset(); + + Ebean.find(Order.class) + .setLazyLoadBatchSize(2) + .findEach(new QueryEachConsumer() { + @Override + public void accept(Order bean) { + doStuff(bean); + } + }); + } + + @Test + public void test_findEach_withFetch() { + + ResetBasicData.reset(); + + Ebean.find(Order.class) + .fetch("details", "id") + .fetch("details.product", "sku") + .fetch("customer") + .fetch("customer.contacts") + .setLazyLoadBatchSize(2) + .findEach(new QueryEachConsumer() { + @Override + public void accept(Order bean) { + doStuff(bean); + } + }); + } + + @Test + public void test_findList() { + + ResetBasicData.reset(); + + List orders = + Ebean.find(Order.class) + .setLazyLoadBatchSize(2) + .findList(); + + for (Order order : orders) { + doStuff(order); + } + } + + @Test + public void test_findList_withFetch() { + + ResetBasicData.reset(); + + List orders = + Ebean.find(Order.class) + .fetch("details", "id") + .fetch("details.product", "sku") + .fetch("customer") + .fetch("customer.contacts") + .setLazyLoadBatchSize(2) + .findList(); + + for (Order order : orders) { + doStuff(order); + } + + new ServerConfig().setLazyLoadBatchSize(); + } + + @Test + public void test_findList_lazyMany() { + + ResetBasicData.reset(); + + List orders = Ebean.find(Order.class) + .setLazyLoadBatchSize(100) + .findList(); + + for (Order order : orders) { + List details = order.getDetails(); + details.size(); + for (OrderDetail detail : details) { + Product product = detail.getProduct(); + product.getSku(); + } + } + } + + private void doStuff(Order order) { + // invoke lazy loading + Customer customer = order.getCustomer(); + System.out.println("do stuff: order:"+order.getId()); + System.out.println("... customer:"+customer.getName()); + + System.out.println("... contacts: "+customer.getContacts().size()+" details:"+order.getDetails().size()); + } +}