どうやら、このバグレポートにある例を使用して、2009 年以降、これを行うことが可能でした:
http://www.avaje.org/bugdetail-92.html
例:
Query<Product> subQuery =
Ebean.createQuery(Product.class)
.select("sku")
.where().idEq(4).query();
List<MinCustomer> list = Ebean.find(MinCustomer.class)
.where().in("name", subQuery)
.findList();
でも:
生成された SQL が無効なため、機能させることができません。Ebean の舞台裏で文字列の置換が行われているため、(少なくとも私にとっては) サブクエリのテーブル名が失われているようです。
サブクエリが「選択している」テーブルへの参照がメインクエリに含まれていることに関係していると思います。
例から有効な SQL を回す:
select c.id, c.name, c.notes
from o_customer c
where (c.name) in (select p.sku from o_product p where p.id = ? )
...私の場合、この無効なSQLに:
select t0.id as c0, ... t0.location_id as c8
from myRecordClass t0
where (t0.location_id) in (
select t0.id
from t0.location_id t0 # should be: from location t0
where t0.customer_id = ?
) and t0.creation > ?
order by t0.creation desc
回避策:
https://stackoverflow.com/a/27431625/190599のように RawSql アプローチを使用します- ここに例を示します:
String sql = "select b.id, b.location_id ... " +
"from myRecordClass b " +
"where location_id in (" +
"select id " +
"from location " +
"where customer_id = " + user.getCustomer().getId() +
") " +
"order by creation desc limit 10";
RawSql rawSql = RawSqlBuilder
.parse(sql)
.columnMapping("b.id", "id")
.columnMapping("b.location_id", "location.id")
....
.create();
Query<MyRecordClass> query = Ebean.find(MyRecordClass.class);
query.setRawSql(rawSql);
final List<MyRecordClass> list = query.findList();