1

私はEBeanを使ってやろうとしています

select * from myTable1 where id not in (select id2 from myTable2) ;

私はtable2オブジェクトにtable1オブジェクトの参照がなく、逆も同じです。EBean を使用してその方法を知っている人はいますか?

今のところ、私が持っているのは次のとおりです。

List<MyTable1> myResult = MyTable1.find.where().eq("id","1" ).findList();

ありがとう。

CC

4

2 に答える 2

2

どうやら、このバグレポートにある例を使用して、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();
于 2015-05-27T21:34:21.887 に答える
0

RawSqlがこの種のクエリを実現する最速の方法であるとはほとんど信じていません。マップされたオブジェクトのリストを返すことができます。

(リファレンス ガイド (PDF)SqlQueryで説明されている)を使用してs のリストを取得することもできます。そのため、マッピングをまったく行わずに必要なデータを見つけることができます。SqlRow

于 2013-06-16T18:58:35.597 に答える