diff --git a/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java b/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java index d7dc2f56a..aeb9ff7dd 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java +++ b/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java @@ -71,7 +71,6 @@ public class LoadManyRequest extends LoadRequest { * This for use when lazy loading is invoked on methods such as clear() and removeAll() where it * generally makes sense to only fetch the Id values as the other property information is not * used. - *

*/ private boolean isOnlyIds() { return onlyIds; @@ -91,18 +90,16 @@ public class LoadManyRequest extends LoadRequest { return loadContext.getBatchSize(); } - private List getParentIdList() { - + private List parentIdList(SpiEbeanServer server) { List idList = new ArrayList<>(); - BeanPropertyAssocMany many = getMany(); for (BeanCollection bc : batch) { idList.add(many.getParentId(bc.getOwnerBean())); + bc.setLoader(server); // don't use the load buffer again } if (many.getTargetDescriptor().isPadInExpression()) { BindPadding.padIds(idList); } - return idList; } @@ -111,9 +108,7 @@ public class LoadManyRequest extends LoadRequest { } public SpiQuery createQuery(SpiEbeanServer server) { - BeanPropertyAssocMany many = getMany(); - SpiQuery query = many.newQuery(server); String orderBy = many.getLazyFetchOrderBy(); if (orderBy != null) { @@ -128,7 +123,7 @@ public class LoadManyRequest extends LoadRequest { } query.setLazyLoadForParents(many); - many.addWhereParentIdIn(query, getParentIdList(), loadContext.isUseDocStore()); + many.addWhereParentIdIn(query, parentIdList(server), loadContext.isUseDocStore()); query.setPersistenceContext(loadContext.getPersistenceContext()); String mode = isLazy() ? "+lazy" : "+query"; @@ -146,7 +141,6 @@ public class LoadManyRequest extends LoadRequest { // override to just select the Id values query.select(many.getTargetIdProperty()); } - return query; } @@ -154,10 +148,8 @@ public class LoadManyRequest extends LoadRequest { * After the query execution check for empty collections and load L2 cache if desired. */ public void postLoad() { - BeanDescriptor desc = loadContext.getBeanDescriptor(); BeanPropertyAssocMany many = getMany(); - // check for BeanCollection's that where never processed // in the +query or +lazy load due to no rows (predicates) for (BeanCollection bc : batch) { @@ -172,6 +164,5 @@ public class LoadManyRequest extends LoadRequest { desc.cacheManyPropPut(many, bc, parentId); } } - } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java index 5035f0802..5d2b781c7 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java @@ -201,7 +201,6 @@ class DLoadManyContext extends DLoadBaseContext implements LoadManyContext { @Override public void loadMany(BeanCollection bc, boolean onlyIds) { - lock.lock(); try { boolean useCache = !onlyIds && context.hitCache && context.property.isUseCache(); @@ -215,6 +214,7 @@ class DLoadManyContext extends DLoadBaseContext implements LoadManyContext { // find it using instance equality - avoiding equals() and potential deadlock issue if (list.get(i) == bc) { list.remove(i); + bc.setLoader(context.parent.getEbeanServer()); return; } } @@ -222,10 +222,9 @@ class DLoadManyContext extends DLoadBaseContext implements LoadManyContext { } } - // Should reduce the list by checking each beanCollection in the L2 first before executing the query - - LoadManyRequest req = new LoadManyRequest(this, onlyIds, useCache); - context.parent.getEbeanServer().loadMany(req); + context.parent.getEbeanServer().loadMany(new LoadManyRequest(this, onlyIds, useCache)); + // clear the buffer as all entries have been loaded + list.clear(); } finally { lock.unlock(); }