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 returnsSqlRow
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.Aggregates:
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
{@code ... // @Sql indicates to that this bean // is based on RawSql rather than a table