mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#360 ENH: Add query setDisableLazyLoading(true) ... for use when graph serialised via external library (and you don't want lazy loading to fire)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlan;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultOrmQueryTest {
|
||||
|
||||
@Test
|
||||
public void testQueryAutofetchHash() throws Exception {
|
||||
|
||||
SpiQuery<?> query1 = (SpiQuery<?>)Ebean.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice", new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
|
||||
query1.queryAutofetchHash(b1);
|
||||
HashQueryPlan hash1 = b1.build();
|
||||
|
||||
SpiQuery<?> query2 = (SpiQuery<?>)Ebean.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
|
||||
query2.queryAutofetchHash(b2);
|
||||
HashQueryPlan hash2 = b2.build();
|
||||
|
||||
assertThat(hash1).isNotEqualTo(hash2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
@@ -15,7 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class TestQueryJoinToAssocOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLazyOnNonLoaded() {
|
||||
public void testQueryJoinOnFullyPopulatedParent() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
@@ -23,6 +24,33 @@ public class TestQueryJoinToAssocOne extends BaseTestCase {
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = Ebean.find(Order.class)
|
||||
.fetch("details", "orderQty, unitPrice", new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = loggedSql.get(1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id c0, t0.id c1,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left outer join o_product t1");
|
||||
assertThat(secondaryQuery).contains(" (t0.order_id) in (?");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testQueryJoinOnPartiallyPopulatedParent() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = Ebean.find(Order.class)
|
||||
.setUseCache(false)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice", new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name")
|
||||
@@ -33,5 +61,81 @@ public class TestQueryJoinToAssocOne extends BaseTestCase {
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = loggedSql.get(1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id c0, t0.id c1,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left outer join o_product t1");
|
||||
assertThat(secondaryQuery).contains(" (t0.order_id) in (?");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryJoinOnPartiallyPopulatedParent_withLazyLoadingDisabled() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = Ebean.find(Order.class)
|
||||
.setUseCache(false)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice", new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
// normally invokes lazy loading
|
||||
order.getOrderDate();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
OrderDetail orderDetail = details.get(0);
|
||||
// normally invokes lazy loading
|
||||
orderDetail.getShipQty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = loggedSql.get(1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id c0, t0.id c1,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left outer join o_product t1");
|
||||
assertThat(secondaryQuery).contains(" (t0.order_id) in (?");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOnPartiallyPopulatedParent_withLazyLoadingDisabled() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = Ebean.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice")//, new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
// normally invokes lazy loading
|
||||
order.getOrderDate();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
OrderDetail orderDetail = details.get(0);
|
||||
// normally invokes lazy loading
|
||||
orderDetail.getShipQty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
|
||||
String originQuery = loggedSql.get(0);
|
||||
assertThat(originQuery).contains("select t0.id c0, t0.status c1, t0.ship_date c2, t1.id c3, t1.order_qty c4, t1.unit_price c5");
|
||||
assertThat(originQuery).contains(" from o_order t0 left outer join o_order_detail t1 ");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user