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();

谢谢。

抄送

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是实现这种查询的最快方法,它允许您返回映射对象的列表。

也可以使用SqlQuery(在参考指南 (PDF)中描述)来获取SqlRows 列表 - 这样您就可以在没有任何映射的情况下找到所需的数据。

于 2013-06-16T18:58:35.597 回答