Queries in Ebean

Partial Objects

Using select() and fetch() you can specify only the properties you wish to fetch.

This can be a very significant performance benefit by only fetching the properties you need. If the properties are in DB indexes then the DB doesn't have to read Index Blocks. This is also a design benefit in that it removes the "fixed" design constraints of using secondary table properties.

Note that Autofetch can use profiling to specify the properties to select rather than you manually doing so (which saves you the work).

example
// a partial object query
// ... only fetch the order id, orderDate and shipDate
List<Order> orders = 
        Ebean.find(Order.class)
                .select("orderDate, shipDate")
                .findList();

Note: the Id property is always fetched even if you do not explicitly include it in the list of properties you want to fetch

<sql summary='[app.data.test.Order]'>
select o.id, o.order_date, o.ship_date 
from or_order o
<sql>
example

*ANY* node of the object graph can be a partial object. In this case both the order and the customer are partially populated.

// a partial object query
// ... only fetch the order id, orderDate and shipDate
// ... only fetch the name of the customer
List<Order> orders = 
        Ebean.find(Order.class)
                .select("orderDate, shipDate")
                .fetch("customer","name")
                .findList();
<sql summary='[app.data.test.Order, customer]'>
select o.id, o.order_date, o.ship_date
        , c.id, c.name 
from or_order o
join or_customer c on o.customer_id = c.id 
<sql>
example

With this example note that the Product associated with each order detail is partially populated (fetching the product id, sku and name).

... again, *ANY* node in the object graph can be partially populated no matter how deep it is in the object graph.

// a mix of partial objects and fully populated objects
List<Order> orders = 
        Ebean.find(Order.class)
                .select("orderDate, shipDate")  // partial
                .fetch("customer","name")  // partial
                .fetch("customer.shippingAddress") 
                .fetch("details")                  
                .fetch("details.product","sku,name") // partial
                .findList();
<sql summary='[app.data.test.Order, customer, customer.shippingAddress]
--               +many[details, details.product]'>
select o.id, o.order_date, o.ship_date
        , c.id, c.name
        , ca.id, ca.line_1, ca.line_2, ca.city, ca.region, ca.cretime, ca.updtime, ca.country_code
        , d.id, d.order_qty, d.ship_qty, d.cretime, d.updtime, d.order_id
        , dp.id, dp.sku, dp.name 
from or_order o
join or_customer c on o.customer_id = c.id 
left outer join or_address ca on c.shipping_address_id = ca.id 
left outer join or_order_detail d on o.id = d.order_id 
left outer join or_product dp on d.product_id = dp.id  
order by o.id
<sql>
woResponse