Module io.ebean.api
Package io.ebean

Interface RawSql

public interface RawSql
Used to build object graphs based on a raw SQL statement (rather than generated by Ebean).

If you don't want to build object graphs you can use SqlQuery instead which returns SqlRow objects rather than entity beans.

Unparsed RawSql:

When RawSql is created via RawSqlBuilder.unparsed(String) then Ebean can not modify the SQL at all. It can't add any extra expressions into the SQL.

Parsed RawSql:

When RawSql is created via RawSqlBuilder.parse(String) then Ebean will parse the SQL and find places in the SQL where it can add extra where expressions, add extra having expressions or replace the order by clause. If you want to explicitly tell Ebean where these insertion points are you can place special strings into your SQL (${where} or ${andWhere} and ${having} or ${andHaving}).

If the SQL already includes a WHERE clause put in ${andWhere} in the location you want Ebean to add any extra where expressions. If the SQL doesn't have a WHERE clause put ${where} in instead. Similarly you can put in ${having} or ${andHaving} where you want Ebean put add extra having expressions.


Often RawSql will be used with Aggregate functions (sum, avg, max etc). The follow example shows an example based on Total Order Amount - sum(d.order_qty*d.unit_price).

We can use a OrderAggregate bean that has a @Sql to indicate it is based on RawSql and not based on a real DB Table or DB View. It has some properties to hold the values for the aggregate functions (sum etc) and a @OneToOne to Order.

Example OrderAggregate

   // @Sql indicates to that this bean
   // is based on RawSql rather than a table

   public class OrderAggregate {

    Order order;

    Double totalAmount;

    Double totalItems;

    // getters and setters

Example 1:

   String sql = " select order_id, o.status,,, sum(l.order_qty*l.unit_price) as totalAmount"
     + " from order o"
     + " join customer c on = o.customer_id "
     + " join order_line l on l.order_id = " + " group by order_id, o.status ";

   RawSql rawSql = RawSqlBuilder.parse(sql)
     // map the sql result columns to bean properties
     .columnMapping("order_id", "")
     .columnMapping("o.status", "order.status")
     .columnMapping("", "")
     .columnMapping("", "")
     // we don't need to map this one due to the sql column alias
     // .columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount")

   List<OrderAggregate> list = DB.find(OrderAggregate.class)
       .where().gt("", 0)
       .having().gt("totalAmount", 20)


Example 2:

The following example uses a FetchConfig().query() so that after the initial RawSql query is executed Ebean executes a secondary query to fetch the associated order status, orderDate along with the customer name.

  String sql = " select order_id, 'ignoreMe', sum(l.order_qty*l.unit_price) as totalAmount "
     + " from order_line l"
     + " group by order_id ";

   RawSql rawSql = RawSqlBuilder.parse(sql)
     .columnMapping("order_id", "")

   List<OrderAggregate> orders = DB.find(OrderAggregate.class)
     .fetch("order", "status,orderDate", FetchConfig.ofQuery())
     .fetch("order.customer", "name")
     .where().gt("", 0)
     .having().gt("totalAmount", 20)


Example 3: tableAliasMapping

Instead of mapping each column you can map each table alias to a path using tableAliasMapping().

   String rs = "select, o.status,,, "+
               ", l.order_qty,, " +
               "from orders o join o_customer c on = o.customer_id " +
               "join order_line l on l.order_id =  " +
               "join product p on = l.product_id  " +
               "where <= :maxOrderId  and = :productId "+
               "order by, asc";

  RawSql rawSql = RawSqlBuilder.parse(rs)
       .tableAliasMapping("c", "customer")
       .tableAliasMapping("l", "lines")
       .tableAliasMapping("p", "lines.product")

  List<Order> ordersFromRaw = DB.find(Order.class)
       .setParameter("maxOrderId", 2)
       .setParameter("productId", 1)


Note that lazy loading also works with object graphs built with RawSql.