al fetch join results in a single SQL query List<Order> list = Ebean.find(Order.class).fetch("details").findList(); // Find Orders join details using a single SQL query
Example: Using a "query join" instead of a "fetch join" we instead use 2 SQL queries
// This will use 2 SQL queries to build this object graph List<Order> list = Ebean.find(Order.class) .fetch("details", new FetchConfig().query()) .findList(); // query 1) find order // query 2) find orderDetails where order.id in (?,?...) // first 100 order id's
Example: Using 2 "query joins"
// This will use 3 SQL queries to build this object graph List<Order> list = Ebean.find(Order.class) .fetch("details", new FetchConfig().query()) .fetch("customer", new FetchConfig().queryFirst(5)) .findList(); // query 1) find order // query 2) find orderDetails where order.id in (?,?...) // first 100 order id's // query 3) find customer where id in (?,?,?,?,?) // first 5 customers
Example: Using "query joins" and partial objects
// This will use 3 SQL queries to build this object graph List<Order> list = Ebean.find(Order.class) .select("status, shipDate") .fetch("details", "quantity, price", new FetchConfig().query()) .fetch("details.product", "sku, name") .fetch("customer", "name", new FetchConfig().queryFirst(5)) .fetch("customer.contacts") .fetch("customer.shippingAddress") .findList(); // query 1) find order (status, shipDate) // query 2) find orderDetail (quantity, price) fetch product (sku, name) where // order.id in (?,? ...) // query 3) find customer (name) fetch contacts (*) fetch shippingAddress (*) // where id in (?,?,?,?,?) // Note: the fetch of "details.product" is automatically included into the // fetch of "details" // // Note: the fetch of "customer.contacts" and "customer.shippingAddress" // are automatically included in the fetch of "customer"
You can use query() and lazy together on a single join. The query is executed immediately and the lazy defines the batch size to use for further lazy loading (if lazy loading is invoked).
List<Order> list = Ebean.find(Order.class) .fetch("customer", new FetchConfig().query(10).lazy(5)) .findList(); // query 1) find order // query 2) find customer where id in (?,?,?,?,?,?,?,?,?,?) // first 10 customers // .. then if lazy loading of customers is invoked // .. use a batch size of 5 to load the customers
Example of controlling the lazy loading query:
This gives us the ability to optimise the lazy loading query for a given use case.
List<Order> list = Ebean.find(Order.class) .fetch("customer","name", new FetchConfig().lazy(5)) .fetch("customer.contacts","contactName, phone, email") .fetch("customer.shippingAddress") .where().eq("status",Order.Status.NEW) .findList(); // query 1) find order where status = Order.Status.NEW // // .. if lazy loading of customers is invoked // .. use a batch size of 5 to load the customers find customer (name) fetch customer.contacts (contactName, phone, email) fetch customer.shippingAddress (*) where id in (?,?,?,?,?)
@author mario
@author rbygrave