|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.avaje.ebean.RawSql
public final class 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(sql) 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(sql) 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
... // @Sql indicates to that this bean // is based on RawSql rather than a table @Entity @Sql public class OrderAggregate { @OneToOne Order order; Double totalAmount; Double totalItems; // getters and setters ...
Example 1:
String sql = " select order_id, o.status, c.id, c.name, sum(d.order_qty*d.unit_price) as totalAmount" + " from o_order o" + " join o_customer c on c.id = o.kcustomer_id " + " join o_order_detail d on d.order_id = o.id " + " group by order_id, o.status "; RawSql rawSql = RawSqlBuilder.parse(sql) // map the sql result columns to bean properties .columnMapping("order_id", "order.id") .columnMapping("o.status", "order.status") .columnMapping("c.id", "order.customer.id") .columnMapping("c.name","order.customer.name") // we don't need to map this one due to the sql column alias //.columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount") .create(); Query<OrderAggregate> query = Ebean.find(OrderAggregate.class); query.setRawSql(rawSql) .where().gt("order.id", 0) .having().gt("totalAmount", 20); List<OrderAggregate> list = query.findList();
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(d.order_qty*d.unit_price) as totalAmount " + " from o_order_detail d" + " group by order_id "; RawSql rawSql = RawSqlBuilder.parse(sql) .columnMapping("order_id", "order.id") .columnMappingIgnore("'ignoreMe'") .create(); Query<OrderAggregate> query = Ebean.find(OrderAggregate.class); query.setRawSql(rawSql) .fetch("order", "status,orderDate", new FetchConfig().query()) .fetch("order.customer", "name") .where().gt("order.id", 0) .having().gt("totalAmount",20) .order().desc("totalAmount") .setMaxRows(10);
Note that lazy loading also works with object graphs built with RawSql.
Nested Class Summary | |
---|---|
static class |
RawSql.ColumnMapping
Defines the column mapping for raw sql DB columns to bean properties. |
static class |
RawSql.Sql
Represents the sql part of the query. |
Method Summary | |
---|---|
RawSql.ColumnMapping |
getColumnMapping()
Return the column mapping for the SQL columns to bean properties. |
RawSql.Sql |
getSql()
Return the Sql either unparsed or in parsed (broken up) form. |
int |
queryHash()
Return the hash for this query. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public RawSql.Sql getSql()
public RawSql.ColumnMapping getColumnMapping()
public int queryHash()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |