是的,它通過RawSql
我允許你使用自己的SQL語句給定的查詢是可能的。從文檔
樣品
// Use raw SQL with an aggregate function
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
// let ebean parse the SQL so that it can
// add expressions to the WHERE and HAVING
// clauses
.parse(sql)
// map resultSet 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")
.create();
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class);
query.setRawSql(rawSql)
// add expressions to the WHERE and HAVING clauses
.where().gt("order.id", 0)
.having().gt("totalAmount", 20);
List<OrderAggregate> list = query.findList();
編輯:我只是檢查養活我的好奇心,也可以(至少在遊戲框架):
Ebean.find(MyModel.class).where().like("concat(subject, ' ' , body)", "%foo%").findList();
雖然我知道這種可能性,我很想堅持Ebean的內置方法。我試着遠離'RawSql',除非沒有別的辦法 – nmagerko
@nmagerko,檢查我的編輯,底部 – biesior
哇,我沒有意識到你可以用這種方式注入自己的原始SQL;我認爲它必須用'RawSql'對象完成。 Nice find – nmagerko