View

We use @View to have an entity map to a database view.

@Entity
@View(name = "order_vw", dependentTables = {"o_order", "o_order_detail"})
public class MyOrderView {
  // fields, accessors etc
}

Note that we do not need to map a @Id property (actually we don't need to map one for a normal entity either).

Dependent Tables

If we enable L2 caching on the entity that is based on a view then we should specify via dependentTables the underlying tables that the view uses. When data for these tables is modified that will automatically invalidate the L2 cache for that view.

Extra DDL to define the view

For ebean to execute DDL to create the database view we need to additional have a extra-ddl.xml. Refer to docs / extra-ddl for more details.


  
  

  
      drop view order_agg_vw if exists;
  

  
    create or replace view order_agg_vw as
    select d.order_id, sum(d.order_qty * d.unit_price) as order_total,
           sum(d.ship_qty * d.unit_price) as ship_total
    from o_order_detail d
    group by d.order_id