Documentation / SQL / Queries
Where raw()
We can use raw()
expressions to add arbitrary SQL expressions to the where
clause of a query.
Order.find.where()
.raw("add_days(orderDate, 10) < ?", someDate)
.findList();
findNative
With findNative()
we give Ebean SQL
. Ebean will automatically map the columns
in the result back to the bean(s).
String nativeSql = "select id, name from customer where name like :some";
List<Customer> customers =
ebeanServer.findNative(Customer.class, nativeSql);
.setParameter("some", "Rob%")
.findList();
RawSql
RawSql
is like findNative except instead of the mapping being automatic we explicitly
define the mapping of how the result columns map to the entity beans.
String rs = "select o.id, o.status, c.id, c.name, " +
" d.id, d.order_qty, p.id, p.name " +
"from orders o join customer c on c.id = o.customer_id " +
"join order_detail d on d.order_id = o.id " +
"join product p on p.id = d.product_id " +
"where o.id <= :maxOrderId and p.id = :productId " +
"order by o.id, d.id asc";
RawSql rawSql = RawSqlBuilder.parse(rs)
.tableAliasMapping("c", "customer")
.tableAliasMapping("d", "details")
.tableAliasMapping("p", "details.product")
.create();
List<Order> ordersFromRaw = Ebean.find(Order.class)
.setRawSql(rawSql)
.setParameter("maxOrderId", 2)
.setParameter("productId", 1)
.findList();
SqlQuery
SqlQuery
is a query that doesn't use entity beans and instead returns SqlRow
objects. As such there is no bean mapping
.
SQL Views